• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in execution of code

 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code tag the next time you'll post some code in your post. It makes it more readable.
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
52 is the ASCII value of 4.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to import this in your code...

import java.io.BufferedReader;
import java.io.InputStreamReader;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic