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.
tianJul 18, 20181. this is a compile time error, but readInt doesn't exist, nextInt does though2. after the second System.out.print, x should be y3. the scanner shouldn't be initialized with a string
wangernestrdragonJul 18, 2018system.in is a string and won't actually do anything.two variables are the same and therefore conflicts each otherin.readInt does not exist
sfolax6776Jul 23, 2018You 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...
mr.rickluJul 23, 2018correct 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);}}
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
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
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...
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);
}
}