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");
1 ответ
+ 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