Computer Science Canada

Very Weird, it will take you 10 secs to solve

Author:  Chinmay [ Sun Jan 06, 2008 7:54 pm ]
Post subject:  Very Weird, it will take you 10 secs to solve

Why wouldnt this work? Sad

code:


void makeItemsList() {
   System.out.print ("Hello");
}

                try {
                        System.out.print("Want me to say hello to you? Y/N   : ");
                        BufferedReader br4 = new BufferedReader(new InputStreamReader(System.in));
                       
                        String decision;
                        decision = br4.readLine();
                        String decision1 = (decision.toUpperCase());
                        System.out.println("\"" + decision1 + "\"");
                       
                        if (decision1 == "Y") {
                                makeItemsList();
                        }
                       
                } catch (IOException u) {}



Why wouldnt this code execute makeItemsList();

Author:  Tony [ Sun Jan 06, 2008 8:02 pm ]
Post subject:  RE:Very Weird, it will take you 10 secs to solve

Because decision1 is a String object and "Y" is _another_ String literal. If they don't point to the same memory location, they are not the same, although they could hold the same value.

To compare against values, you should use .equals()

Author:  Chinmay [ Sun Jan 06, 2008 8:38 pm ]
Post subject:  Re: Very Weird, it will take you 10 secs to solve

oh yes i forgot, thx man Very Happy

very forgetfull Wink


: