0

Okay, I am a newbie java programmer, and am trying to create some java code to read a configuration file that I have created, and return the results. I will give you the code first, then explain what I am trying to do:

public static String read(String property, File file) throws IOException{

    FileInputStream in = new FileInputStream(file);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line;
    String r = "ERROR";
    while ((line = br.readLine()) != null) {
        if(!line.startsWith("//")){
            String[] split = line.split(" ");
            if(split[0] == property){
                r = split[1];
            }
        }
    }

    br.close();

    return r;

}

This is what the actual configuration file looks like, where "hello" is the property, and "world" is the value:

hello world

To explain, the desired property is taken as a parameter, as well as the location of theconfiguration file. It will then read each line (ignoring "//") and split it into the property name and value. It will then return the value if it was found, or "ERROR" if the property could not be found.

I then run the following code:

System.out.println(Property.read("hello", new File("config/confix.txt")));

However, every time I run this code, "ERROR" will be printed. I have checked and re-checked my code, and still can't think of what I have done wrong.

Apologies if I have not made myself very clear, so do ask if I need to explain better!

Callum B
  • 21
  • 1
  • 2

1 Answers1

0

String comparison must be done using the equals method not operator

Replace this

split[0] == property

with this

split[0].equals(property)

Try to learn how to use the debugger, it will save you a lot of time.

Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
  • Thanks! Problem is I am programming a plugin for a server, and I am yet to find out how to debug for that. – Callum B Nov 12 '14 at 19:53
  • there should always be way to test your code locally, either write a main that uses your code or write unit tests – Logan Murphy Nov 12 '14 at 19:58