Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Very Weird, it will take you 10 secs to solve
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Chinmay




PostPosted: 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();
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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()
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Chinmay




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: