Input from Console

Discussion in 'Java' started by subhabangalore@gmail.com, Feb 28, 2013.

  1. Guest

    Dear Group,

    I am trying to learn Java. I was trying to write a code for input from Console.

    I found two ways to do it. One is by using import java.io.*; and the other is,
    import java.util.Scanner;

    If I assign a class name and start the console input from public static void main
    both works fine.

    But as I want to declare one function for it and like to call it in main body.

    I am not being able to do that.

    If any one of the learned members can kindly suggest it.

    Regards,
    Subhabrata.
     
    , Feb 28, 2013
    #1
    1. Advertising

  2. Arne Vajhøj Guest

    On 2/28/2013 9:28 AM, wrote:
    > I am trying to learn Java. I was trying to write a code for input from Console.
    >
    > I found two ways to do it. One is by using import java.io.*; and the other is,
    > import java.util.Scanner;
    >
    > If I assign a class name and start the console input from public static void main
    > both works fine.
    >
    > But as I want to declare one function for it and like to call it in main body.
    >
    > I am not being able to do that.
    >
    > If any one of the learned members can kindly suggest it.


    Could you post the code that is not working?

    Arne
     
    Arne Vajhøj, Feb 28, 2013
    #2
    1. Advertising

  3. Guest

    On Thursday, February 28, 2013 8:04:08 PM UTC+5:30, Arne Vajhøj wrote:
    > On 2/28/2013 9:28 AM, wrote:
    >
    > > I am trying to learn Java. I was trying to write a code for input from Console.

    >
    > >

    >
    > > I found two ways to do it. One is by using import java.io.*; and the other is,

    >
    > > import java.util.Scanner;

    >
    > >

    >
    > > If I assign a class name and start the console input from public staticvoid main

    >
    > > both works fine.

    >
    > >

    >
    > > But as I want to declare one function for it and like to call it in main body.

    >
    > >

    >
    > > I am not being able to do that.

    >
    > >

    >
    > > If any one of the learned members can kindly suggest it.

    >
    >
    >
    > Could you post the code that is not working?
    >
    >
    >
    > Arne


    Dear Sir,
    Thank you for your kind reply. The Code is as follows:

    import java.io.*;
    import java.util.Scanner;
    public class AddressBook {
    public static void main(String[] args) {
    name();
    System.out.print("\n");
    name1();

    }
    public static String name(){
    String s1="Subhabrata";
    String s2="Banerjee";
    System.out.print("The First Name is:"+s1);
    System.out.println("\n");
    System.out.print("The Surname Is:"+s2);
    return s1;
    }
    public static void name1(){
    String name;
    int age;
    Scanner in = new Scanner(System.in);

    name = in.nextLine();
    age=in.nextInt();
    in.close();
    System.out.println("Name :"+name);
    System.out.println("Age :"+age);





    }
    }

    It is compiling and running fine but if I want to give any input it is giving error as,
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at AddressBook.name1(AddressBook.java:24)
    at AddressBook.main(AddressBook.java:7)

    Regards,
    Subhabrata.
     
    , Feb 28, 2013
    #3
  4. FredK Guest

    On Thursday, February 28, 2013 6:47:47 AM UTC-8, wrote:
    > On Thursday, February 28, 2013 8:04:08 PM UTC+5:30, Arne Vajhøj wrote: > On 2/28/2013 9:28 AM, wrote: > > > I am trying to learn Java. I was trying to write a code for input from Console. > > > > > > I found two ways to do it. One is by using import java.io.*; and the other is, > > > import java.util.Scanner; > > > > > > If I assign a class name and start the console input from public static void main > > > both worksfine. > > > > > > But as I want to declare one function for it and like tocall it in main body. > > > > > > I am not being able to do that. > > > > > > If any one of the learned members can kindly suggest it. > > > > Could you post the code that is not working? > > > > Arne Dear Sir, Thank you foryour kind reply. The Code is as follows: import java.io.*; import java.util.Scanner; public class AddressBook { public static void main(String[] args) { name(); System.out.print("\n"); name1(); } public static String name(){String s1="Subhabrata"; String s2="Banerjee"; System.out.print("The First Name is:"+s1); System.out.println("\n"); System.out.print("The Surname Is:"+s2); return s1; } public static void name1(){ String name; int age; Scanner in = new Scanner(System.in); name = in.nextLine(); age=in.nextInt(); in.close(); System.out.println("Name :"+name); System.out.println("Age :"+age); } } It is compiling and running fine but if I want to give any input it is giving error as, Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at AddressBook.name1(AddressBook.java:24) at AddressBook.main(AddressBook.java:7) Regards, Subhabrata.


    What did you type as input?
    If the second line was not an integer, you will get that error.
     
    FredK, Feb 28, 2013
    #4
  5. Roedy Green Guest

    On Thu, 28 Feb 2013 06:28:23 -0800 (PST),
    wrote, quoted or indirectly quoted someone who said :

    >I found two ways to do it. One is by using import java.io.*; and the other is,
    >import java.util.Scanner;


    there is also out.println and in.readln.

    See http://mindprod.com/applet/fileio.html for sample code.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    One thing I love about having a website, is that when I complain about
    something, I only have to do it once. It saves me endless hours of
    grumbling.
     
    Roedy Green, Feb 28, 2013
    #5
  6. Guest

    On Thursday, February 28, 2013 9:14:30 PM UTC+5:30, Roedy Green wrote:
    > On Thu, 28 Feb 2013 06:28:23 -0800 (PST),
    >
    > wrote, quoted or indirectly quoted someone who said :
    >
    >
    >
    > >I found two ways to do it. One is by using import java.io.*; and the other is,

    >
    > >import java.util.Scanner;

    >
    >
    >
    > there is also out.println and in.readln.
    >
    >
    >
    > See http://mindprod.com/applet/fileio.html for sample code.
    >
    > --
    >
    > Roedy Green Canadian Mind Products http://mindprod.com
    >
    > One thing I love about having a website, is that when I complain about
    >
    > something, I only have to do it once. It saves me endless hours of
    >
    > grumbling.


    Thank you Sir. My input was wrong.
    Regards,
    Subhabrata.
     
    , Feb 28, 2013
    #6
  7. markspace Guest

    On 2/28/2013 6:47 AM, 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.
     
    markspace, Feb 28, 2013
    #7
  8. Arne Vajhøj Guest

    On 2/28/2013 10:44 AM, Roedy Green wrote:
    > On Thu, 28 Feb 2013 06:28:23 -0800 (PST),
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> I found two ways to do it. One is by using import java.io.*; and the other is,
    >> import java.util.Scanner;

    >
    > there is also out.println and in.readln.
    >
    > See http://mindprod.com/applet/fileio.html for sample code.


    in.readln is that a new thing in Java 9 alpha version or??

    Arne
     
    Arne Vajhøj, Feb 28, 2013
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Finney
    Replies:
    2
    Views:
    1,027
    Egor Bolonev
    Jun 30, 2003
  2. Egor Bolonev
    Replies:
    0
    Views:
    500
    Egor Bolonev
    Jun 30, 2003
  3. Serge Terekhoff
    Replies:
    0
    Views:
    661
    Serge Terekhoff
    Jun 30, 2003
  4. Miktor
    Replies:
    7
    Views:
    12,195
    Joe Bacigalupa
    Aug 9, 2005
  5. Replies:
    5
    Views:
    3,470
    James Kanze
    Mar 4, 2008
Loading...

Share This Page