-
How can I read two double after typing?
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.
-
The System.in.read() method doesn't read data as text, it reads raw data from the console. In other words, if you were to type 1, you would get the ASCII value of '1', not 1. This said, there are several alternatives for reading data from files (or the console) as text. First, you can get the Scanner class which I notice a lot of people using, Its new to Java 1.5, so I don't know a whole lot about it, but you can check the man pages (http://java.sun.com/j2se/1.5.0/docs/.../Scanner.html). Alternatively, you can used BufferedReader and Double.parseDouble(), but this requires that you know things like both numbers are on the same line and stuff like that. For strictly formatted input, it works well, but otherwise it is kinda difficult to use. If both numbers were going to be on the same line, you might do something like this:
Code:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String ln = in.readLine();
StringTokenizer st = new StringTokenizer(ln);
double x = Double.parseDouble(st.nextToken());
double y = Double.parseDouble(st.nextToken());
Hope that helps.
~evlich
-
Thank you!
Hey!
Thank you very much for your help, I'm going to try your suggestion.
Thank you for your help.
Bye
Similar Threads
-
Replies: 5
Last Post: 05-27-2008, 11:17 AM
-
By WackoWolf in forum C++
Replies: 4
Last Post: 10-17-2005, 04:22 PM
-
Replies: 3
Last Post: 10-02-2005, 11:57 PM
-
Replies: 1
Last Post: 09-26-2005, 05:09 PM
-
By George Gilbert in forum vb.announcements
Replies: 0
Last Post: 08-19-2001, 11:34 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks