Read section 4.1.3 on page 145 first, then complete R4.12 on page 164. Please feel free to copy and paste the code below to Codiva and try.
correct code:
import java.util.Scanner;
public class HasError
{
public static void main(String[] args)
{
int x = 0;
int y = 0;
Scanner in = new Scanner(System.in);
System.out.print("Please enter an integer:");
x = in.nextInt();
System.out.print("Please enter another integer: ");
y = in.nextInt();
System.out.print("The sum is ");
System.out.print(x + y);
}
}
You all get some of the corrections, but not ALL of them. If you test your corrections in Codiva, you will see what else is missing...
system.in is a string and won't actually do anything.
two variables are the same and therefore conflicts each other
in.readInt does not exist
1. this is a compile time error, but readInt doesn't exist, nextInt does though
2. after the second System.out.print, x should be y
3. the scanner shouldn't be initialized with a string