• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

reading from keyBoard

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hell0!

I have tried to run this program.
I have entered value of x as 1,but it printed x as 49 & automaticaly printed y some value.
I want to enter x as 1 & y as 2.i want to get same results.
HOW?
import java.io.*;
class Ex8
{
public static void main(String a[]) throws IOException
{
System.out.println("enter a number");
try{
System.out.println("enter x");
int x;
x=System.in.read();
System.out.println("x is"+x);
System.out.println("enter y");
int y;
y=System.in.read();
System.out.println("y is"+y);
}
catch(Exception e)
{
System.out.println(e);
}

}
}

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gururaj kulkarni:
Hell0!

I have tried to run this program.
I have entered value of x as 1,but it printed x as 49 & automaticaly printed y some value.
I want to enter x as 1 & y as 2.i want to get same results.
HOW?
import java.io.*;
class Ex8
{
public static void main(String a[]) throws IOException
{
System.out.println("enter a number");
try{
System.out.println("enter x");
int x;
x=System.in.read();
System.out.println("x is"+x);
System.out.println("enter y");
int y;
y=System.in.read();
System.out.println("y is"+y);
}
catch(Exception e)
{
System.out.println(e);
}

}
}


First of all:
What is happenning is that when you enter a 1 for x, it is returning 49, which is the standard function value of 1 (in decimal format) in ASCII code. If you would have declared x as Hexadecimal it would have returned 31, and if Octal 060.
The reason that you are getting a 13 on y is that the value for carriage return in ASCII code is 13 in decimal format.
So, what you are doing is entering a 1 and then hitting Enter(carriage return).
I hope this helps to explain what is going on.
There is a program that I found that reads in a double from the keyboard and adds two to it. The program is in "Core JAVA 2, Volume 1-Fundamentals", by Cay S. Horstmann and Gary Cornell.
It goes as follows:
import java.io.*;
import java.text.*;
public class ReadDoubleTest
{
// shows how to read a double the hard way
public static void main(String[] args)
{
System.out.println("Enter a number, I'll ad two to it.");
double x; // the number we wish to read
try
{
InputStreamReader isr = new InputStreamReader
(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(s);
x = n.doubleValue();
}
catch(IOException e)
{
x = 0;
}
catch(ParseException e)
{
x = 0;
}
System.out.println(x + 2);
}
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EE,
Do my homework also? lol.
 
gururaj kulkarni
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANKS, Eric

From ur answer i got what is happening?
I have gone through ur example.Just for reading x we have to do so much?Suppose i want to read 10 different numbers of different types,then u can imagine the complexity? Is reading from keyBoard is so much complex?.
Also if u r free try to give solution to my problem that i have given in first letter.
with regards..
....Gururaj
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't have to be complicated, you can place this in it's own method(s) to obtain your input and convert it to the desired type as necessary. One example is

You can add this to any class or have a special class of your own and call it from there.
[This message has been edited by Carl Trusiak (edited September 18, 2000).]
reply
    Bookmark Topic Watch Topic
  • New Topic