Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

why Scanner class not working ?

user575089Dec 29 2017 — edited Dec 30 2017

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 ?

Comments

mNem

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

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-7404

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

2

This 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.

user575089

the line terminator is a token and is still there.

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

user575089

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

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

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

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

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-7404

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.

1 - 10
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 27 2018
Added on Dec 29 2017
10 comments
3,740 views