How does this program actually work...?
import java.util.Scanner;
class string
{
public static void main(String a[]){
int a;
String s;
Scanner scan = new Scanner(System.in);
System.out.println("enter a no");
a = scan.nextInt();
System.out.println("no is ="+a);
System.out.println("enter a string");
s = scan.nextLine();
System.out.println("string is="+s);
}
}
The output is:
enter the no
1234
no is 1234
enter a string
string is= //why is it not allowing me to enter a string here?
This question is related to
java
user-input
java.util.scanner
You only need to use scan.next()
to read a String
.
Simple solution to consume the \n character:
import java.util.Scanner;
class string
{
public static void main(String a[]){
int a;
String s;
Scanner scan = new Scanner(System.in);
System.out.println("enter a no");
a = scan.nextInt();
System.out.println("no is ="+a);
scan.nextLine();
System.out.println("enter a string");
s = scan.nextLine();
System.out.println("string is="+s);
}
}
s=scan.nextLine();
It returns input was skipped.
so you might use
s=scan.next();
use a temporary scan.nextLine();
this will consume the \n character
Incase you don't want to use nextint, you can also use buffered reader, where using inputstream
and readline
function read the string.
Don't try to scan text with nextLine(); AFTER using nextInt() with the same scanner! It doesn't work well with Java Scanner, and many Java developers opt to just use another Scanner for integers. You can call these scanners scan1 and scan2 if you want.
.nextInt()
gets the next int
, but doesn't read the new line character. This means that when you ask it to read the "next line", you read til the end of the new line character from the first time.
You can insert another .nextLine()
after you get the int
to fix this. Or (I prefer this way), read the int
in as a string
, and parse it to an int
.
import java.util.*;
public class ScannerExample {
public static void main(String args[]) {
int a;
String s;
Scanner scan = new Scanner(System.in);
System.out.println("enter a no");
a = scan.nextInt();
System.out.println("no is =" + a);
System.out.println("enter a string");
s = scan.next();
System.out.println("string is=" + s);
}
}
This is because after the nextInt() finished it's execution, when the nextLine() method is called, it scans the newline character of which was present after the nextInt(). You can do this in either of the following ways:
You can use the next method on the scanner object as
scan.next();
This is a common misunderstanding which leads to confusion if you use the same Scanner for nextLine() right after you used nextInt().
You can either fix the cursor jumping to the next Line by yourself or just use a different scanner for your Integers.
OPTION A: use 2 different scanners
import java.util.Scanner;
class string
{
public static void main(String a[]){
int a;
String s;
Scanner intscan =new Scanner(System.in);
System.out.println("enter a no");
a=intscan.nextInt();
System.out.println("no is ="+a);
Scanner textscan=new Scanner(System.in);
System.out.println("enter a string");
s=textscan.nextLine();
System.out.println("string is="+s);
}
}
OPTION B: just jump to the next Line
class string
{
public static void main(String a[]){
int a;
String s;
Scanner scan =new Scanner(System.in);
System.out.println("enter a no");
a = scan.nextInt();
System.out.println("no is ="+a);
scan.nextLine();
System.out.println("enter a string");
s = scan.nextLine();
System.out.println("string is="+s);
}
}
Scanner's buffer full when we take a input string through scan.nextLine(); so it skips the input next time . So solution is that we can create a new object of Scanner , the name of the object can be same as previous object......
if you don't want to use parser :
int a;
String s;
Scanner scan = new Scanner(System.in);
System.out.println("enter a no");
a = scan.nextInt();
System.out.println("no is =" + a);
scan.nextLine(); // This line you have to add (It consumes the \n character)
System.out.println("enter a string");
s = scan.nextLine();
System.out.println("string is=" + s);
Source: Stackoverflow.com