2

I wanted to write a small quiz with an int calculation, but I can not get it to run.

import java.util.Scanner;

public class Quiz {
    public static void main(String[]args) {
        Scanner scan = new Scanner(System.in);
        int a = 4;
        int b = 4;
        String in;
        in= scan.nextLine();
        System.out.println("What is 4+4?");
        if (in.equals (a+b)) {
            System.out.println("Correct");
        }else {
            System.out.println("Wrong");
        }
    }
}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

5 Answers5

1

You can use scan.nextInt(); to accept int values,

Scanner scan = new Scanner(System.in);
int a = 4;
int b = 4;
int in= scan.nextInt();
System.out.println("What is 4+4?");
if (in == (a+b)) {
    System.out.println("Correct");
}else {
    System.out.println("Wrong");
}
Vikas
  • 6,868
  • 4
  • 27
  • 41
1

First of all why would you add two numbers when you can keep one number in a variable as the expected result. The input expects a String so you can either change it to int or say String.valueOf(expected result)

int in= scan.nextInt();

Scanner scan = new Scanner(System.in);
        final int a = 8;
        System.out.println("What is 4+4?");
        String in= scan.nextLine();
        if (in.equals(String.valueOf(a))) {
            System.out.println("Correct");
        }else {
            System.out.println("Wrong");
        }
    }
}
letMeThink
  • 81
  • 1
  • 8
1

Although others suggest using Scanner#nextInt I wouldn't recommend it. It has some kind of nonintuitive behavior and causes harm when used by programmers who are new to Java when trying to read first number, the operator and the second number.

As the OP is already familiar with Scanner#nextLine I would suggest the following approach:

Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();

int first = Integer.parseInt(a);
int second = Integer.parseInt(b);

int result = first+second;

To design such quiz, I would suggest going a bit further:

public static void main(String[] args) {
    Random random = new Random();
    Scanner sc = new Scanner(System.in);

    int a = random.nextInt(100);
    int b = random.nextInt(100);
    System.out.println(String.format("What is the result of %d + %d", a, b));

    String answer = sc.nextLine();
    try {
        int result = Integer.parseInt(answer);
        if (result == a+b) {
            System.out.println("Correct");
        } else {
            System.out.println("Wrong");
        }
    } catch (NumberFormatException e) {
        System.out.println(answer + " - is not a valid number");
    }
}
xenteros
  • 15,586
  • 12
  • 56
  • 91
0
String in = scan.nextLine();
if (in.equals(String.valueOf(a+b))) {
  System.out.println("Correct");
}else {
  System.out.println("Wrong");
}

Or use Integer.toString(a+b) or "" + (a + b)

Or change Integer in = scan.nextInt()

AnnKont
  • 424
  • 4
  • 17
0

You need to convert the String value of the the scanner to an int, you can use scan.nextInt() as well but this can lead to problems later with the scanner not progressing to the next line after the int. You also need to order it correctly otherwise it'll ask for input before the question is asked. See below, hope it helps.

import java.util.Scanner;

public class Quiz {
    public static void main(String[]args) {
        Scanner scan = new Scanner(System.in);
        int a = 4;
        int b = 4;
        int in;

        System.out.println("What is 4+4?");

        in = Integer.valueOf(scan.nextLine());

        if (in == (a+b)) {
            System.out.println("Correct");
        }else {
            System.out.println("Wrong");
        }
    }
}
ChaseVsGodzilla
  • 102
  • 1
  • 1
  • 5