+ 1
Driver licence challenge
What's wrong with Driver Licence challenge , the output of the the ssecond string is null (i'm using java) By the way i reported the problem but nothing is fixed , maybe i'm missing something HELP https://www.sololearn.com/coach/18?ref=app
5 Antworten
+ 1
This is a common problem, and it happens because the nextInt method doesn't read the newline character of your input, so when you issue the command nextLine, the Scanner finds the newline character and gives you that as a line.
A workaround could be this one:
System.out.println("Enter id");
id1 = in.nextInt();
in.nextLine(); // skip the newline character System.out.println("Enter name"); name1 = in.nextLine();
Another way would be to always use nextLine wrapped into a Integer.parseInt:
int id1;
try {
System.out.println("Enter id");
id1 = Integer.parseInt(input.nextLine()); } catch (NumberFormatException e) { e.printStackTrace(); } System.out.println("Enter name"); name1 = in.nextLine();
+ 3
Afir Sofiane , post the link to your code in Playground, so somebody can check it.
+ 2
My code : import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
String myName=scan.nextLine();
int numberAgent=scan.nextInt();
String otherName=scan.nextLine();
System.out.print(otherName );
}
}
As you can see "otherName" return null
+ 1
kannan. Thank you bro it works now 💪
+ 1
Afir Sofiane welcome!