Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2008

Tip: Looking for answers? Try searching our database.

displaying prompts and reading user input question

Thread view: 
Jason Hodge - 28 Feb 2008 14:41 GMT
The following code was copied from  O'Reilly's Java Examples in a
Nutshell. I slightly modified it to display the prompt and print out that
I wanted. My problem is that rather than displaying the "name>" prompt
and then waiting for user input, my program seems to want user input and
then displays a prompt. I am using Netbeans as my IDE.

Thanks for any help,
Jason

package test;
import java.io.*;

public class test {    
   public static void main(String[  ] args) throws IOException {
       // This is how we set things up to read lines of text from the
user.
       BufferedReader in=new BufferedReader(new InputStreamReader
(System.in));
       // Loop forever
       for(;;) {
           // Display a prompt to the user
           System.out.print("name> ");
           // Read a line from the user
           String line = in.readLine( );
           // If we reach the end-of-file,  
           // or if the user types "quit", then quit
           if ((line == null) || line.equals("quit")) break;
           // Try to parse the line, and compute and print the factorial
           try {  
               int x = Integer.parseInt(line);
               System.out.println("yay");  
           }
           // If anything goes wrong, display a generic error message
           catch(Exception e) { System.out.println("Invalid Input"); }
       }
   }
}

Output:

init:
deps-jar:
Compiling 1 source file to /home/Test/build/classes
compile:
run:
jason
name> Invalid Input

In the above output, I would think that it should be:

run:
name> Jason
GArlington - 28 Feb 2008 17:17 GMT
> The following code was copied from  O'Reilly's Java Examples in a
> Nutshell. I slightly modified it to display the prompt and print out that
[quoted text clipped - 49 lines]
> run:
> name> Jason

Try to build it and run then, it maybe a weird problem with system.out
in your IDE...
TheBigPJ - 28 Feb 2008 17:24 GMT
This might not help but after running that from the command line (a
dos prompt) it seemed to work as you expected it to.

I got told myself when learning avoid using a java enviroment, use the
basics (basic word editor and command line) as you might not learn why
your doing something in an enviroment just that the way you do it
works. And also not every company will have the same enviroment that
you will be using.

Peter
Mark Space - 28 Feb 2008 18:11 GMT
> The following code was copied from  O'Reilly's Java Examples in a
> Nutshell. I slightly modified it to display the prompt and print out that
> I wanted. My problem is that rather than displaying the "name>" prompt
> and then waiting for user input, my program seems to want user input and
> then displays a prompt. I am using Netbeans as my IDE.

What version of NetBeans?  5.x was a little weird that way.

The first thing you need to understand is how IO works.  While 5.x is
weird, it's not buggy.  ANY system (your OS for example) may delay IO
operations and perform them in any order to make the IO more efficient.
   If you need to guarantee things happen in a certain order, then you
need to force it by calling flush() to force the write to the screen.

Try calling System.out.flush() right after the print statement, see if
that fixes it up.

You also might try NetBeans 6.  I think they changed the way their
console works in 6.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.