Forum Stats

  • 3,825,167 Users
  • 2,260,475 Discussions
  • 7,896,430 Comments

Discussions

why Scanner class not working ?

user575089
user575089 Member Posts: 466
edited Dec 30, 2017 12:18AM in Java Programming

my keyboard inputs // first an integer and second a string

2

This is a string

my code :

import java.util.Scanner;

public class MyClass {

    public static void main(String args[]) {

    Scanner sc=new Scanner(System.in) ;

   int a=sc.nextInt();

    //float b=sc.nextFloat();

    String str=sc.nextLine();

   

     System.out.println("###");

    System.out.println(a);

    // System.out.println(b);

      System.out.println(str);

}

}

I am not getting two inputs . I am getting 2 only.  what happened to second input ?

rpc1

Answers

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Dec 29, 2017 11:59AM

    Try your input from one line.

    10 my string input

    Check the documentation and has number of examples.

    https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html 
  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Dec 29, 2017 12:44PM

    import java.util.Scanner;

    public class B {

       public static void main(String args[]) {

          String strInput = "10\n01--some text for testing...\n02--second text string";

          Scanner sc = new Scanner(strInput);

          sc.useDelimiter("\n");

          int a = sc.nextInt();

          String str1 = sc.next();

          String str2 = sc.next();

          System.out.println("### Input:");

          System.out.println(strInput);

          System.out.println("### Output:");

          System.out.println(a);

          System.out.println(str1);

          System.out.println(str2);

       }

    }

    ### Input:

    10

    01--some text for testing...

    02--second text string

    ### Output:

    10

    01--some text for testing...

    02--second text string

  • Unknown
    edited Dec 29, 2017 12:54PM
    my keyboard inputs // first an integer and second a string2This is a string. . .int a=sc.nextInt();. . .String str=sc.nextLine();I am not getting two inputs . I am getting 2 only. what happened to second input ?

    It is still there waiting for you to 'get' it.

    The 'nextint' gets a TOKEN - it does NOT get a line - the line terminator is a token and is still there.

    The 'nextLine' gets the rest of the line - which is the line terminator of the first line.

    So ask yourself - why did you use 'nextint' for the first input but 'nextLine' for the next one?

    I suggest you read The Java Tutorials trail named 'Scanning'.

    https://docs.oracle.com/javase/tutorial/essential/io/scanning.html

    Try the example in that trail to understand how it works.

    rpc1
  • user575089
    user575089 Member Posts: 466
    edited Dec 29, 2017 11:23PM
    the line terminator is a token and is still there.

    Could not get this part. Could you please explain bit more at this ?

  • user575089
    user575089 Member Posts: 466
    edited Dec 29, 2017 11:14PM

    I'd send inputs as I have shown  ( but not through a single line.)

    2

    This is a string.

    how do I take into values ?

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Dec 29, 2017 11:16PM

    import java.util.Scanner;

    public class B {

       public static void main(String args[]) {

         

          System.out.println(">> example - 1");

          Scanner sc = new Scanner(System.in);

          int a = sc.nextInt();

          //https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine--

          sc.nextLine(); //clue from rp0428

          String str1 = sc.nextLine();

          System.out.println("### Output:");

          System.out.println(a);

          System.out.println(str1);

          System.out.println("\n>> example - 2");

          sc = new Scanner(System.in);

          a = Integer.parseInt(sc.nextLine());

          str1 = sc.nextLine();

          System.out.println("### Output:");

          System.out.println(a);

          System.out.println(str1);

          System.out.println("\n>> example - 3");

          sc = new Scanner(System.in);

          sc.useDelimiter("\n");

          a = Integer.parseInt(sc.next());

          str1 = sc.next();

          System.out.println("### Output:");

          System.out.println(a);

          System.out.println(str1);

       }

    }

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Dec 29, 2017 11:28PM

    import java.util.Scanner;

    public class B {

      

          public static void main(String args[]) {

          Scanner sc = new Scanner(System.in);

          sc.useDelimiter("\n");

          int a = sc.nextInt();

          String str1 = sc.next();

          System.out.println("### Output:");

          System.out.println(a);

          System.out.println(str1);

       }

  • user575089
    user575089 Member Posts: 466
    edited Dec 29, 2017 11:54PM

    if I input this way :

    2 this is a test

    output:

    2

    <space>this is a test

    I was not expecting <space> there.  What could be the reason ?

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Dec 30, 2017 12:07AM

    That is because, everything after the int value (2) is returned as nextLine() as described in the API documentation.

    https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine-- 

    nextLine

    public String nextLine()

    Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. 

    Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

    Returns:
    the line that was skipped

    Try this for explanation:

    import java.util.Scanner;

    public class B {

      

          public static void main(String args[]) {

          Scanner sc = new Scanner(System.in);

          sc.useDelimiter("mydelimiter");

          int a = sc.nextInt();

          String str1 = sc.nextLine();

          System.out.println("### Output:");

          System.out.println(a);

          System.out.println(str1);

       }

    }

    100mydelimiter text string

    ### Output:

    100

    mydelimiter text string

  • Unknown
    edited Dec 30, 2017 12:18AM

    Could not get this part. Could you please explain bit more at this ?

    Did you go through the Java Tutorial trail?

    Did you try that example?

    Did you understand what a 'token' is? And that some methods read 'tokens'?

    Your first line has two tokens. The nextInt method reads ONE TOKEN so gets the 2.

    The nextLine method reads the rest of the line - which is the line terminator token.

    Call 'nextLine' again and it will read the second line.

    Go thru the tutorial and read the API docs for the methods you are using.

This discussion has been closed.