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 / General / November 2006

Tip: Looking for answers? Try searching our database.

Best way to take text input from console

Thread view: 
Angus - 16 Nov 2006 19:04 GMT
Hello

I am fairly new to Java and want to start with simple console programs (ie
text based - not GUI).

I can output easily using System.out.println  - but been searching web as to
how to get input from console and finding all sorts of different ways.  What
is the elegant way?  Or most accepted/standard/easiest way to do it?

I thought I should be able to use something like System.in.read ?  No?

Angus
martin.lansler@gmail.com - 16 Nov 2006 19:12 GMT
Try this:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Sysin {
   public static void main(String[] args) throws Exception {
       BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
       System.out.println("Enter you name: ");
       String name = br.readLine();
       System.out.println("Hello " + name);
   }
}

Regards,
Martin.

> Hello
>
[quoted text clipped - 8 lines]
>
> Angus
martin.lansler@gmail.com - 16 Nov 2006 19:15 GMT
One more thing...

In Java 5 one can make a static import:
import static java.lang.System.out;

allowing one to write:
out.println("Enter you name: ");

/Martin Lansler

martin.lans...@gmail.com wrote:
> Try this:
>
[quoted text clipped - 26 lines]
> >
> > Angus
Rhino - 16 Nov 2006 19:13 GMT
> Hello
>
[quoted text clipped - 8 lines]
>
> I thought I should be able to use something like System.in.read ?  No?

As you've noticed, there are several ways that work and are in common usage.

One approach that seems quite popular for those on Java 1.5 or later is to
use the Scanner class. I know a guy who is just learning Java at college and
they use Scanner for all their console I/O.

--
Rhino
Taria - 17 Nov 2006 11:02 GMT
> One approach that seems quite popular for those on Java 1.5 or later is to
> use the Scanner class. I know a guy who is just learning Java at college and
> they use Scanner for all their console I/O.
>
> --
> Rhino

I'm just learning java this semester and Scanner is pretty quick and
easy to use.  The way to implement this into your program is to do the
following things:

1) type this line at the top of your program before any variables,
classes, main body is declared:
  import java.util.Scanner;

2)  You can use the API manual that will tell you all the methods that
are connected with Scanner but here are examples how you can use it to
take in input from a user.

        Scanner input = new Scanner(System.in);
        System.out.print("Enter your sentence: ");
        String sentence = input.nextLine();

If you want to read in just a word and not a series of words, then
change "nextLine" to "next".  To input an integer then use for example:

          System.out.print("\nEnter the choice, followed by 2
integers:  ");
           String choice = input.next();

          // evaluate choice then input 2 integers, expected

           if (choice.equalsIgnoreCase("q")) {
               quit = true;
              System.out.println ("Adios mi amigo.");
           }
           else
           {
              int x = input.nextInt();
              int y = input.nextInt();
           // ... morec ode here
           }


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



©2009 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.