Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why the code is not working as expected?

 
khoo bee feong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main(String[] args) throws IOException{

InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);

do {
String s=br.readLine();
}while(s!="happy");


}

why s cannot be resolved into a variable?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch khoo bee feong!
because that's how scope is defined in java. you are declaring a variable inside {} and the variable lives only inside {}.
you cant use that variable in condition(while parentheses)


it make sense to me . if below code is allowed then you will end up in infinity loop.
 
khoo bee feong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you. I got my infinity loop done ! But unfortunately the loop doesn't stop when I type "happy" in the console. Please advice.

static String s;


public static void main(String[] args) throws IOException{

InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);

do {
s=br.readLine();
}while(s!="happy");


}
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it should be declared outside of the loop.

Also your test condition will not work.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While that works, it's not a great solution. [Edit - this was a reply to Khoo's last message, not Wesleigh's]

There's a general principle that it's better to keep the scope of variables as small as possible, to avoid conflicts and confusion. Your original code did this perfectly, except that the scope was too small, because it's not in scope during the while condition.

But making it a static variable is going to the other extreme - you've now made the scope as large as possible. A better solution would be to just declare the variable immediately before the do/while loop.

Oh, you also need to change the while condition - you should never use == or != to compare strings as it won't do what you want most of the time. Use the equals() method instead. That's why your code doesn't work. So I'd suggest this:

 
khoo bee feong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much everyone.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khoo bee feong wrote: But unfortunately the loop doesn't stop when I type "happy" in the console. Please advice.

did you hit enter after the typing ?
 
khoo bee feong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
khoo bee feong wrote: But unfortunately the loop doesn't stop when I type "happy" in the console. Please advice.

did you hit enter after the typing ?


Wesleigh and Matthew answers the question already. It's because my condition not working. :P
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
khoo bee feong wrote: But unfortunately the loop doesn't stop when I type "happy" in the console. Please advice.

did you hit enter after the typing ?


that is not the issue. as said it is his condition.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, that's fine
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic