Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 6th, 2007, 1:54 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Old Jul 6th, 2007, 2:17 AM   #2
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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?
357mag is offline   Reply With Quote
Old Jul 6th, 2007, 2:20 AM   #3
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4 lectricpharaoh will become famous soon enough
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:
Originally Posted by 357mag
Well I guess if you change it to say println instead of just print then it works. Although I don't understand why.
Hence it being a bug.
Quote:
Originally Posted by 357mag
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?
The first time you said 'input box', I dismissed it, but now you've said it again. I don't know what you're referring to. Are you running this through some IDE, or something? If so, have you tried running it from the command line?
__________________
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.
lectricpharaoh is offline   Reply With Quote
Old Jul 6th, 2007, 3:18 AM   #4
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Old Jul 6th, 2007, 8:16 AM   #5
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old Jul 6th, 2007, 8:44 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC