+ 1

Whats wrong with this code plz tell

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); public int x{ int x=sc.nextLine(); if(x==1){ System.out.println ("hi"); } else{ System.out.println ("do something"); } } } }

28th Sep 2017, 1:07 PM
Abinesh N
Abinesh N - avatar
4 Antworten
+ 2
Whats wrong with this code plz tell (EDITED) import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int x=sc.nextInt(); if(x==1){ System.out.println ("hi"); } else { System.out.println ("do something"); } } }
28th Sep 2017, 1:22 PM
D_Stark
D_Stark - avatar
+ 1
change nextLine() to nextInt() also delete public int x {} as you have already initialized int x to scanner
28th Sep 2017, 1:15 PM
D_Stark
D_Stark - avatar
0
why intx=sc.nextint() in int ..in string we use nextline() whats the difference
28th Sep 2017, 1:32 PM
Abinesh N
Abinesh N - avatar
0
nextLine() reads the remainder of the current line even if it is empty. nextInt() reads an integer but does not read the escape sequence "\n". But dont worry too much about this just know nextInt() is for int's and nextLine() is for Strings.
28th Sep 2017, 1:38 PM
D_Stark
D_Stark - avatar