Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #22627

Re: Input from Console

From markspace <markspace@nospam.nospam>
Newsgroups comp.lang.java.programmer
Subject Re: Input from Console
Date 2013-02-28 08:42 -0800
Organization A noiseless patient Spider
Message-ID <kgo1c9$kp8$1@dont-email.me> (permalink)
References <c9d678fe-b7f0-4757-8d4c-ea7454f8b2fb@googlegroups.com> <512f6ae1$0$285$14726298@news.sunsite.dk> <6d604145-dbbe-4f89-87c8-7e8180dfbd6a@googlegroups.com>

Show all headers | View raw


On 2/28/2013 6:47 AM, subhabangalore@gmail.com wrote:

>         Scanner in = new Scanner(System.in);
>
> 	   name = in.nextLine();
> 	   age=in.nextInt();


I'm having a very hard time using Scanner this way.  I think it's 
perhaps because I normally wrap System.in in a BufferedReader.  But 
let's in general introduce a third method of reading console input, 
which is to use a BufferedReader.

       BufferedReader input = new BufferedReader(
                     new InputStreamReader( System.in ) );

Now you can read lines from the console, which is a little more 
intuitive and also works as many users expect.  Then the trick is to 
extract an integer (or whatever input you are looking for) from the line 
of text, which can be done with Scanner.

Full code:

package quicktest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class InputTest {
    public static void main( String[] args ) throws IOException {
       BufferedReader input = new BufferedReader(
                     new InputStreamReader( System.in ) );
       int age =  -1;
       do {
          System.out.println( "Please enter your age");
          String line = input.readLine();
          Scanner scan = new Scanner( line );
          if( scan.hasNextInt() ) {
             age = scan.nextInt();
             break;
          }
       } while( true );
       System.out.println( "Age: "+age );
    }
}

Study this carefully, and try entering some bad input when it asks for 
age.  I think you'll see how it works.  This could be made more pithy, I 
think, but I'll leave it like this because I think it's easier to trace 
for someone who's just starting out.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Input from Console subhabangalore@gmail.com - 2013-02-28 06:28 -0800
  Re: Input from Console Arne Vajhøj <arne@vajhoej.dk> - 2013-02-28 09:34 -0500
    Re: Input from Console subhabangalore@gmail.com - 2013-02-28 06:47 -0800
      Re: Input from Console FredK <fred.l.kleinschmidt@gmail.com> - 2013-02-28 07:32 -0800
      Re: Input from Console markspace <markspace@nospam.nospam> - 2013-02-28 08:42 -0800
  Re: Input from Console Roedy Green <see_website@mindprod.com.invalid> - 2013-02-28 07:44 -0800
    Re: Input from Console subhabangalore@gmail.com - 2013-02-28 08:21 -0800
    Re: Input from Console Arne Vajhøj <arne@vajhoej.dk> - 2013-02-28 16:55 -0500

csiph-web