This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Problem in execution of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in execution of code" Watch "Problem in execution of code" New topic
Author

Problem in execution of code

Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
I am facing a problem in following code:



When i am giving an integer value to k, which is satisfying the while loop condition, for example 4, the code is not executing properly. Means while loop is not getting executed & i am getting the output of last print statement "You are out of all loops".
Is there any expression lack in code??


Regards,
Vishnu
Baiju Scariah
Ranch Hand

Joined: Mar 17, 2005
Posts: 33
Just print the value of k and check

int k=System.in.read();
System.out.println("Value of "+k);

Returns next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Please use the code tag the next time you'll post some code in your post. It makes it more readable.


[My Blog]
All roads lead to JavaRanch
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Baiju Scariah wrote:Just print the value of k and check

int k=System.in.read();
System.out.println("Value of "+k);

Returns next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.


I gave a value 4 to k & when i printed it , its giving the output as 52. What is reason for it. Any suggestions?/
Rahul Kurup
Greenhorn

Joined: Dec 11, 2009
Posts: 9
52 is the ASCII value of 4.


Rahul J.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38436
    
  23
Beware of using the System.in.read() method. It doesn't do what you think it does. Always read the API documentation before using an unfamiliar method.

If you look at the link I posted, you see its return value is int. It says "next byte" so it won't return anything over 0xff (255). If you use a keyboard with only ASCII keys, you get the ASCII equivalent of the keystroke, eg 0x34 for '4' (52 in decimal). Try casting those ints to chars, and printing them with the %c tag of a printf method. Then they will revert to the characters you thought they were.
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Campbell Ritchie wrote:Beware of using the System.in.read() method. It doesn't do what you think it does. Always read the API documentation before using an unfamiliar method.

If you look at the link I posted, you see its return value is int. It says "next byte" so it won't return anything over 0xff (255). If you use a keyboard with only ASCII keys, you get the ASCII equivalent of the keystroke, eg 0x34 for '4' (52 in decimal). Try casting those ints to chars, and printing them with the %c tag of a printf method. Then they will revert to the characters you thought they were.


Ohk. But in the code k is an integer value. The Read() method is returning ASCII equivalent. So is there any type of cast or other way by which we can change the return type from ASCII to int or character, according to requirement??
Nehel Patel
Greenhorn

Joined: Feb 09, 2010
Posts: 21

You can use below code to read integer value

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = bf.readLine();
int k = Integer.parseInt(str);
while(k<10){
//Your code
}
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Nehel Patel wrote:
You can use below code to read integer value

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = bf.readLine();
int k = Integer.parseInt(str);
while(k<10){
//Your code
}


Which package i need to import for using the class BufferReader??
Nehel Patel
Greenhorn

Joined: Feb 09, 2010
Posts: 21
You need to import this in your code...

import java.io.BufferedReader;
import java.io.InputStreamReader;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in execution of code