aspose file tools*
The moose likes Java in General and the fly likes why the code is not working as expected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why the code is not working as expected?" Watch "why the code is not working as expected?" New topic
Author

why the code is not working as expected?

khoo bee feong
Greenhorn

Joined: Nov 15, 2012
Posts: 4
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 15, 2012
Posts: 4
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

Joined: Sep 04, 2012
Posts: 81
Because it should be declared outside of the loop.

Also your test condition will not work.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

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

Joined: Nov 15, 2012
Posts: 4
thank you so much everyone.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 15, 2012
Posts: 4
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

Joined: Sep 04, 2012
Posts: 81
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

Joined: Jan 28, 2008
Posts: 5575

Oh, that's fine
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why the code is not working as expected?