Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

system.in.read method  RSS feed

 
revival revival
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i have to use the system.in.read method to read a character )grade A, B, etc) and display the corresponding marks range.
I have done the following but i am having 'loss of precision' error.
can someone tell me the correct way of inputting a char by sytem.in.read method and it gets out a char and not an integer.
also would like to know how to get the standinpout package as i can't find it.



import java.io.*;

public class CharacterInputb


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

{
System.out.println("Please enter a grade character: A, B, C, or D:");

char grade = System.in.read();
System.out.println ("\t Grade \t Mark Range");
if (grade = A){
System.out.println ("\t A \t 80 - 100");
}else if (grade = B){
System.out.println ("\t B \t 70 - 79");
}else if (grade = C){
System.out.println ("\t C \t 55 -69");
}else if (grade = D){
System.out.println ("\t D \t 45 - 54");
}else {
System.out.println ("Wrong character inputted!!!");
}

}
}
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. Please check the naming policy and adjust your displayed name.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.in.read(), and all other InputStream / Reader read methods, return an integer.
For Reader this is because it needs -1 (outside the range of char) to indicate the stream has ended.
For InputStream all bytes are transformed from the -127-to-128 range to the 0-to-255 range, where all negative values get 256 added. This is, again, to allow -1 to be the end-of-stream identifier.
 
Mikaela Loi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is how you should have written your programme at the first place:



import java.io.*;

public class CharacterInputb


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

{
char grade;
System.out.println("Please enter a grade character: A, B, C, or D:");

grade = (char)System.in.read();
System.out.println ("\t Grade \t Mark Range");
if (grade == 'A')
{
System.out.println ("\t A \t 80 - 100");
}
else if (grade == 'B')
{
System.out.println ("\t B \t 70 - 79");
}
else if (grade == 'C')
{
System.out.println ("\t C \t 55 -69");
}
else if (grade == 'D')
{
System.out.println ("\t D \t 45 - 54");
}
else
{
System.out.println ("Wrong character inputted!!!");
}
}
}
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!