views:

71

answers:

2

I'm writing a loop that will exit itself when the scanner receives the string "end". Though, when i type in end the loop continues. so if file = the input, then if(file=="end") is false, even though i typed in end! It's driving me crazy and i'd very much appreciate if anyone could give their thoughts on where this has went wrong. This is not part of a homework assignment or anything, i am doing it for kicks - so don't worry about me asking you to do my work or anything. The code is below.

String file = "";
    Scanner in = new Scanner(System.in);
    ArrayList<Integer> fileInput = new ArrayList<Integer>(); 

    while(file!="end") {
        // Scan for filename/end program
        System.out.println("Provide the name of a file in the \"bin/\" folder, i will assume it's .txt");
        file = in.nextLine();

        System.out.println("." + file + ".");
        if(file!="end") {
            file= "bin/" + file + ".txt";

            // start reading
            try {
                // If file found then carry on
                BufferedReader openFile = new BufferedReader(new FileReader(file));
                fileInput = readIn(openFile);
                int lowerBound = getLower(fileInput);
                int upperBound = getUpper(fileInput);

                System.out.println("Lower Bound: " + lowerBound);
                System.out.println("Upper Bound: " + upperBound);

                // file not found
            } catch (FileNotFoundException e) {
                System.out.println("File not found!");
            }
        }
    }
    System.out.println("Goodbye!");
    System.exit(0);

Thanks!

+4  A: 

In Java, you have to use .equals() for string equality; otherwise it does a reference comparison.

String s1 = "end";
String s2 = "end";  // different string in memory
s1 == s2            // false: not the same string
s1.equals(s2)       // true: have the same characters
"end".equals(s1)    // also true
"end" == s1         // false

And yeah, it sucks.

Anthony Mills
he has `(!(file.equals("end")))` in his code
Anthony Forloney
Actually, all String literals are interned, so in this case `s1 == s2` will return true.
danben
Ah this is the problem. I had tried the .equals but used it wrongly! Thank you.
Graeme
In your example, `s1` might (and probably will) be equal to `s2`, as danben said. A better example would be: `String s1 = new String("end"); String s2 = new String("end");`
Eli Acherkan
+2  A: 

I think your problem is here:

if(file!=file2) {
    file= "bin/" + file + ".txt";

Unless you enter "end" twice, you overwrite file before the next check.

Also, I think you want

if(!file.equals(file2)) {
    file= "bin/" + file + ".txt";

Edit: in response to your comment, just changing from == "end" to .equals("end") should do it.

danben
Sorry, i gave you the wrong code. The code i actually am working with is above. That line should just be file!="end". same with the while statement.
Graeme
Even at that, you should have `if (!file.equals("end"))`
Anthony Forloney