Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jul 31st, 2007, 12:57 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Clearing the input box

I don't know if any of you guys have used Netbeans or Java Studio Creator, but I'm experimenting with both. I've got this program that inputs two integers, computes the sum, and displays the result:

public class HelloWorld
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        int x;
        int y;
        
        System.out.println("Enter a value for x: ");
        
        x = input.nextInt();
        System.out.println("Enter a value for y: ");
        y = input.nextInt();
        
        int sum = x + y;
        
        System.out.println("The sum is: " + sum);               
    }
}

What I can't figure out is every time I re-run the program, the previous integer is still sitting in the input box. Let's say I run the program and the first integer I enter is 3. Then I hit the Enter key to send it along. Then for thte second integer I hit 9. Then I hit the Enter key to send it along. The program displays 12 and I'm done.

Now I want to run the program again, and when I hit the Run button, the number 9 is still sitting in the input box. This is quite annoying. It shouldn't be there. Seems to me when you re-run the program the box should be clear, ready to accept new input.

By the way, this is not a GUI program, this is a console program. But there is a long input box on the bottom of the IDE window when you write a program that uses a scanner to get input from the keyboard.
357mag is offline   Reply With Quote
Old Jul 31st, 2007, 1:09 AM   #2
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Well I think I may have found a way to fix it. If I go into the Build menu before each new run, and hit Clear and Build, then re-run the program it seems to work.

But you know if you remove the println statements and change them to just print, it doesn't show up on the output until the whole program is done. In other words, when you use println("Enter a value for x: ) it will show on the output window:

Enter a value for x:

But if you remove the println and just code it System.out.print("Enter a value for x: ")

That sentence doesn't show up at all in the output window. You can still enter in two values for the variables, but the strings and the result will not show up until the program is completely done executing.
357mag is offline   Reply With Quote
Old Jul 31st, 2007, 3:00 AM   #3
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
what IDE are you using? try running the program from console.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Jul 31st, 2007, 6:06 AM   #4
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
The situation happens in both Netbeans and Sun Java Studio Creator. It doesn't happen in JDeveloper or JCreator.
357mag is offline   Reply With Quote
Old Jul 31st, 2007, 6:28 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It's the same problem that was pointed out to you in your "Can't make the Scanner thing work" post. Consequently, the answer is the same.
__________________
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
Old Nov 1st, 2007, 9:28 PM   #6
casesensitive
Programmer
 
Join Date: Oct 2007
Posts: 41
Rep Power: 0 casesensitive is on a distinguished road
Re: Clearing the input box

System.out.flush()
casesensitive 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
error 3 Expected end-of-statement m0rb1d Other Scripting Languages 0 Dec 12th, 2006 10:05 AM
Reading character input into an array (raw mode) shoeyfighter C 3 Nov 2nd, 2006 3:49 PM
User input jayme C++ 38 Nov 25th, 2005 7:27 PM
Checking form input Jerry Java 1 Oct 31st, 2005 8:41 AM




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

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