• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

system.in.read method

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!");
}

}
}
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch. Please check the naming policy and adjust your displayed name.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags.
reply
    Bookmark Topic Watch Topic
  • New Topic