![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
Can't make the Scanner thing work
I'm using Java 6 and I'm trying to input two integers from the keyboard and then output the sum. Here is what I got:
import java.util.Scanner;
public class GettingInput {
public static void main(String args[])throws IOException
{
Scanner s = new Scanner(System.in);
System.out.print("Enter first integer: ");
int x = s.nextInt();
System.out.print("Enter second integer: ");
int y = s.nextInt();
int sum = x + y;
System.out.println("The sum is " + sum);
}
}But when I run the program it says "cannot find symbol" and it highlights the public static void line. When I remove the throws IOException part, the program runs but it runs all wrong. It doesn't prompt me to enter a number, yet there still is the little box at the bottom to enter a number. If I enter the first number, and hit Enter, then enter the second number, then hit Enter, the program works, but then after all that it shows the prompts about entering the first integer and entering the second integer. It's all backwards. Need a bit of help here. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
Well I guess if you change it to say println instead of just print then it works. Although I don't understand why.
I have another question though. Every time I run the program, the previous first integer value is still sitting in the input box. Is there any way to make it so when you start the program anew that the input box is clear? |
|
|
|
|
|
#3 | ||
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4
![]() |
I've run into a similar problem. It seems that the Java libraries (or possibly the VM) have a bug whereby the output stream and input stream are tied together. If you put a newline at the end of the prompts, or use println() instead of print(), it should work as you intend. I've tried flushing the output stream, to no avail. In my case, the bug manifested by the input not working unless it was printing a newline character; the symptom was an endless loop. Now, why changing what the output should affect the input is beyond me; hence my thinking that it's a bug. It also makes it impossible to have a proper prompt where the user enters stuff on the same line as the prompt.
[edit] I just tried your code; it works fine for me. Go figure. [/edit] Quote:
Quote:
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp Last edited by lectricpharaoh; Jul 6th, 2007 at 2:37 AM. |
||
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
I'm using Netbeans. Whenever you re-run the program, the previous first value that you entered is still sitting in the input box. But when I do the same program in JDeveloper, the input box is clear every time you re-run the program. I've been experimenting with lots of IDE's seeing which one I like best.
|
|
|
|
|
|
#5 |
|
Sexy Programmer
|
By the way, the IOException class is in the java.io package. You must import it in order to use that class. Just throwing it out there.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Run it from the command line, as lectric suggested. NetBeans is trapping your output and input to put it in the li'l windows, and getting it wrong.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| major class project | Dark Flare Knight | Java | 9 | Apr 29th, 2005 2:45 PM |
| How do I make this work? simple problem | gardon | C++ | 5 | Feb 15th, 2005 7:34 AM |
| 40 Things you'd like to say out loud at work | big_k105 | Coder's Corner Lounge | 11 | Jan 25th, 2005 2:13 AM |
| how do i make custom functions | cloud- | Visual Basic | 3 | Jan 11th, 2005 1:14 PM |