in line 15, why we need to write twice of it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

in line 15, why we need to write twice of it?

public static void main(String[] args) { Scanner scn=new Scanner(System.in); int i; String str; char ch; System.out.print("input a number: "); i=scn.nextInt(); System.out.print("You inputed: " +i+"\n input a character: "); str=scn.nextLine(); //this one str=scn.nextLine(); System.out.print("you inputed: "+str); ch=str.charAt(0); System.out.print("\n you inputed: "+ch+" "+((int)ch-64)+"th character");

13th Mar 2023, 3:12 AM
naaaatt18
naaaatt18 - avatar
1 Answer
+ 4
When you take inputs using nextLine() method then cursor moves to the next line and a blank string (which is new line) will be taken as input so you cannot take next inputs like integer or character. It will throw an exception. So to avoid taking new line as an input we used extra nextLine() method
13th Mar 2023, 3:52 AM
A͢J
A͢J - avatar