Driver licence challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

9th Aug 2020, 12:47 PM
Afir Sofiane
Afir Sofiane - avatar
5 Answers
+ 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();
9th Aug 2020, 12:56 PM
kannan
kannan - avatar
+ 3
Afir Sofiane , post the link to your code in Playground, so somebody can check it.
9th Aug 2020, 12:48 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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
9th Aug 2020, 12:48 PM
Afir Sofiane
Afir Sofiane - avatar
+ 1
kannan. Thank you bro it works now 💪
9th Aug 2020, 12:59 PM
Afir Sofiane
Afir Sofiane - avatar
+ 1
Afir Sofiane welcome!
9th Aug 2020, 1:01 PM
kannan
kannan - avatar