• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I read two double after typing?  RSS feed

 
Atreo De Maio
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!!

Thank you for reading me, I need your help.
I'm new (very very new) in JAVA programming.

I have a problem. I'm trying to write an application that reads a couple of
numbers (double). It shows a message on screen that says something like
'type a number', and then it reads it.

The thing is that the program doesn't stop for the input of the second
number and I don't know how to solve it.

Can you help me please?

My code is similiar to the following one:

System.out.println("Type the first number: ");
try{
x = System.in.read();
}catch(IOException e){
System.out.println("It didn't work.");
}

System.out.println("Type the second number: ");
try{
y = System.in.read();
}catch(IOException e){
System.out.println("It didn't work.");
}
// We build the point
Point point1 = new Point(x,y);


Thank you very much for your attention and help.
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, try something like this:
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also be careful that you understand the methods that you call.
For example, the API for read states that it:

Reads the next byte of data from the input stream.

Binary streams are different from character streams. If you type 123,
System.in.read() will not return 123, but typically 49, the ASCII code
value for the character '1'.
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr Albrechtsen's reply is of course very appropriate, for the benefit of Mr De Maio, I comment my previous given code:

InputStream (System.in)reads bytes, so less suitable for text and numbers,
we need something to read characters: a Reader (in my code a bufferedReader to read as efficiently as possible)
An InputStreamReader is a bridge from byte streams (read be InputStream)to character streams(read by Reader)

Like a plumber, we fix the pipes one after the other:
InputStream is = System.in;//read bytes
InputStreamReader isr = new InputStreamReader(is);//bytes to characters
BufferedReader br = new BufferedReader(isr);//read and store characters

the last thing "reads a line of text", and stops when it encounters <ENTER>
Then we try to extract of double from what is read...

Happy reading!
 
Atreo De Maio
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

Thanks a lot to everyone!!!


I'm going to try your suggestions.


Thank you for your help.

Bye
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why inflict BufferedReaders on those who have come new to Java after J5.0?


Find out about the java.util.Scanner class, which is much much easier to use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!