Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How can I read two double after typing?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, try something like this:
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey!

Thanks a lot to everyone!!!


I'm going to try your suggestions.


Thank you for your help.

Bye
 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic