Close
Close

Regarding Scanner Class

   Ashwith

import java.util.*;
public class Input {

    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.println("Enter something");
        int a=s.nextInt();
        System.out.println("The thing entered is:"+a);
        Scanner w=new Scanner(System.in);
        System.out.println("Now enter a string");
        String q=s.nextLine();
        System.out.println("The entered String is:"+q);
        
    }

}
**In the above program the output is:

Enter something
54
The thing entered is:54
Now enter a string
The entered String is:
**can I get the explanation for this behaviour

 


Answers

  •   

    Don’t press enter before entering the string input. On pressing enter, the string entered in the new line will not be taken as input and hence the input is taken as blank.


    • did u actually compiled it.
      - Ashwith
    • just compile it once for me and explain me the reason for the behaviour of the program
      - Ashwith
    • thanks for the reply though
      - Ashwith

Ask Yours
Post Yours
Write your answer