+ 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"); } } } }
4 odpowiedzi
+ 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");
}
}
}
+ 1
change nextLine() to nextInt()
also delete public int x {} as you have already initialized int x to scanner
0
why intx=sc.nextint() in int ..in string we use nextline() whats the difference
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.