Author Topic: Reading files and processing contents  (Read 4332 times)

0 Members and 1 Guest are viewing this topic.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Reading files and processing contents
« on: November 06, 2012, 11:34:32 am »
I have a txt file with a few names of channel operators in it and I want to use it for my bot, so that if a command is ordered, first it checks if that person is op.
The txt file simply has each name on a seperate line. If I run the isOp() method in main outside of anything, it works fine. I can print out the names, check them, do whatever I want. But for some reason when using it in the bots class, it won't work.
It is supposed to return true when the name is found but the check in line 51 (isOp(sender)) doesn't seem to work. My name is in the file, but yet it does not enter the if statement. Am I doing something terribly wrong here?
Code: [Select]
package bot;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import org.jibble.pircbot.PircBot;

public class HelpBot extends PircBot{
public HelpBot(){
this.setName("AmA");
}

public void onJoin(String channel, String sender, String login, String hostname, String message){
if (!(sender.equalsIgnoreCase(this.getNick()))){
sendMessage(channel, "Hello "+sender+"! Enjoy your stay here :)");
}else{
sendMessage(channel, sender+" is up and running again :)");
}
}
public void onMessage(String channel, String sender, String login, String hostname, String message){
//Operator commands. Settings etc.
if (message.startsWith("!")){
//Owner only commands (me :P)
if (sender.equals("@tikker") || sender.equals("tikker")){
if (message.equalsIgnoreCase("!stop")){
System.out.println("Stopping AmA :(");
sendMessage(channel, "Out of fuel, all systems shutting down :(");
this.disconnect();
System.exit(0);
}
if (message.equalsIgnoreCase("!test")){
sendMessage(channel, "====== Running tests now ======");
sendMessage(channel, "====== Starting message test...");
sendMessage(channel, "Hello World!");
sendMessage(channel, "====== Starting PM test...");
sendMessage("Franz24", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage("Gorok", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage("Agent321", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage("tousent", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage("tikker", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage(channel, " ");
sendMessage(channel, "Tests succesfully ran");
sendMessage(channel, "====== Stopping tests ======");
}
}
//Regular OP commands
if (message.contains("!nick")){
System.out.println("Person "+sender+" tried to order !nick.");
try {
if (isOp(sender) == true){
System.out.println(sender+" changed AmA's nick.");
String newNick = message.substring(6);
this.changeNick(newNick);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
if (message.equalsIgnoreCase("!topic")){
try {
if (isOp(sender)){
String newTopic = message.substring(8);
this.setTopic(channel, newTopic);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
//User commands. Questions etc.
if (message.startsWith(">")){
if (message.equalsIgnoreCase(">AmA")){
sendMessage(sender, "I'm "+this.getNick()+" the question bot. I'm here to help you with questions about Galaxy55 :) I'm created by tikker. Should there be any trouble, contact him.");
}
if (message.equalsIgnoreCase(">wiki")){
sendMessage(sender, "The wiki can be found at http://www.galaxy55wiki.com");
}
}
}

public void onPing(String sourceNick, String sourceLogin, String sourceHostname, String target, String pingValue) {
sendMessage(sourceNick, "PONG");
System.out.println("Received PING from "+sourceNick+" sent PONG");
}

/*
* User defined methods
*/
//Checks if a name is listed in ops.txt
public static boolean isOp(String name) throws FileNotFoundException{
File opsFile = new File(System.getProperty("user.dir")+"\\src\\ops.txt");
Scanner scan = new Scanner(opsFile);
String line = scan.nextLine();

while (scan.hasNextLine() && line != name){
line = scan.nextLine();
System.out.println(line);
if (name == line){
System.out.println(line);
System.out.println(line == name);
return true;
}
}
return false;
}
}


« Last Edit: November 06, 2012, 01:29:56 pm by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline merthsoft

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 241
  • Rating: +63/-1
    • View Profile
Re: Reading files and processing contents
« Reply #1 on: November 06, 2012, 12:42:49 pm »
Have you hooked it up to a debugger and stepped through the code to make sure it's doing what you expect? That's where I'd start--that or using debug output statement to see what's going on. Check the name being passed in, check the file name, check each line, etc.
Shaun

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Reading files and processing contents
« Reply #2 on: November 06, 2012, 01:55:22 pm »
For some reason the line is not just a string or something like that. If I say line = "tikker"; it all works fine. However using the toString() method on the scanner also doesn't work.

Some people need a high five in the face... with a chair.
~EC

Offline merthsoft

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 241
  • Rating: +63/-1
    • View Profile
Re: Reading files and processing contents
« Reply #3 on: November 06, 2012, 01:59:59 pm »
You didn't answer my questions. Have you used a debugger? Have you used debug outputs? What have you done to find what's wrong? You said the code works fine outside of the bot, so there's gotta be something strange the bot is doing.

Looking a little deeper at it, though, if your name is the first one in the list, It think it will return false because of your boolean in the while.
Shaun

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Reading files and processing contents
« Reply #4 on: November 06, 2012, 02:01:56 pm »
I did run it through a debugger. That's what lead me to finding out that somehow if line == tikker, and name == tikker, line does not equal name.

[edit]
I think it will return false because of your boolean in the while.
Doesn't return just terminate the whole method?
« Last Edit: November 06, 2012, 02:03:07 pm by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline merthsoft

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 241
  • Rating: +63/-1
    • View Profile
Re: Reading files and processing contents
« Reply #5 on: November 06, 2012, 02:04:37 pm »
When comparing strings, I suggest using ".equals()" rather than "==". Try that and see if it fixes it.

And here's a good explanation:
http://stackoverflow.com/a/513839/698871
« Last Edit: November 06, 2012, 02:06:41 pm by merthsoft »
Shaun

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Reading files and processing contents
« Reply #6 on: November 06, 2012, 02:11:27 pm »
Ah using name.equals(line) did the trick, thanks!

Some people need a high five in the face... with a chair.
~EC

Offline merthsoft

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 241
  • Rating: +63/-1
    • View Profile
Re: Reading files and processing contents
« Reply #7 on: November 06, 2012, 02:14:39 pm »
Glad it helped. I always forget that java is like that...
Shaun