1

In the following code, the block under if(timesout[entry] == "exit") will never execute. I have verified timesout[entry] for the current loop is set as "exit" in debugging mode, as well as by printing out the variable before the if statement is evaluated, but no matter what, the block never executes when I enter exit at the prompt, and I am stumped as to why.

import java.util.Scanner;

public class timetracker {
public static void main(String args[]) {
    boolean exit = false;
    String[] reasons = new String[30];
    String[] timesout = new String[30];
    String[] timesin = new String[30];
    int entry = 0;
    Scanner keyinput = new Scanner(System.in);

    recordloop:
    while(exit == false) {
        //record info



        System.out.println("Enter time out:");
        timesout[entry] = keyinput.nextLine();

        if(timesout[entry] == "exit") {
            exit = true;
            break recordloop;   
        }

        System.out.println("Enter reason:");
        reasons[entry] = keyinput.nextLine();
        System.out.println("Enter time in:");
        timesin[entry] = keyinput.nextLine();

        entry = entry + 1;

    }

    System.out.println("Times away from phone:\n ----- \n");
    int count = entry;
    entry = entry + 1;

    while(count < entry) {
        System.out.println(reasons[count] + ": " + timesout[count] + " - " + timesin[count] + "\n");
        count = count + 1;
    }
}
}
Morgan
  • 867
  • 3
  • 11
  • 34
  • 2
    for string always use .equals() to compare. – Dinup Kandel Sep 03 '12 at 05:48
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Makoto Sep 03 '12 at 05:48
  • The [Java tag wiki](http://stackoverflow.com/tags/java/info) has a small collection of common issues. Comparing strings is one of many common issues that Java programmers come across. – Makoto Sep 03 '12 at 05:48

3 Answers3

9
timesout[entry] == "exit"

use equals() to compare String, == compares reference equality

See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Wow. I'm a c# programmer so I'd have struggled with this too. So does this imply that java doesn't use immutable strings? Don't worry, I'll google it ;-) – Sepster Sep 03 '12 at 05:49
  • 3
    java does, but it doesn't cache all the String in pool, so its better to use equals() to compare the Object equality – jmj Sep 03 '12 at 05:51
  • Ah yes, a quick google search revealed that too, thanks. It's strange that comparing a string variable against a string literal doesn't cause a compiler warning, then? I mean, it's perfectly clear what the programmer is trying to do (and I can't think of a scenario where I'd want to check if my variable references the same string that was created for me in "the back end" to represent my literal). – Sepster Sep 03 '12 at 05:54
  • Thank you! I'm a PHP Programmer by nature so I wasn't sure what was going on here. – Morgan Sep 03 '12 at 05:58
  • String caches instance in literal pool, where you could simply compare to see if they are equal then both of the Object are same and hence equals – jmj Sep 03 '12 at 06:01
4

Instead of

if(timesout[entry] == "exit") 

use

if(timesout[entry].equals("exit"))

or

if("exit".equals(timesout[entry]))

more information different between == and equals()

 http://stackoverflow.com/questions/12171783/how-is-it-possible-for-two-string-objects-with-identical-values-not-to-be-equal/12171818#12171818 
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
1

Can you try

if("exit".equals(timesout[entry]))

instead of

if(timesout[entry] == "exit")

As @Jigar Joshi pointed you should see the difference and meaning of == and equals()

Community
  • 1
  • 1
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63