Q:

creating class from small program

i worked on a little multiple choice programm as seen here
    import java.util.Arrays;
    import java.util.Scanner;

    public class logic  {

public static void main(String[] args) {

    String question = "Welche Fabe hat der Frosch?"; //Your Question here

    //Possible Answers
    String a = "Gelb";
    String b = "Rot";
    String c = "Grün";

    String correct = "c"; //Right Answer

    String [] arr = new String[5];

    arr[0] = question;
    arr[1] = a;
    arr[2] = b;
    arr[3] = c;
    arr[4] = correct;

    System.out.println(arr[0] + "\n");
    System.out.println("Answer a: " + arr[1]);
    System.out.println("Answer b: " + arr[2]);
    System.out.println("Answer c: " + arr[3] + "\n");

    int i = 0;
    while (i != 1) {
    Scanner userin = new Scanner(System.in);

    System.out.println("Type a,b,c for correct answer.\n");

    String selected = userin.next();

    if (selected.equals(correct)) {
        i = 1;
        System.out.println("\nCorrect!");
    }else {
        System.out.println("Try again");
    }
    }
}
}

I thought i could create a class based on that. And that's were the trouble starts.
This is what i got so far.
import java.util.Scanner;

public class MultipleChoice {

    private String question, a, b, c, correct;

    public void setQuestion(String question) {
      this.question = question;
    }

    public void setA(String a) {
      this.a = a;
    }

    public void setB (String b) {
      this.b = b;
    }

    public void setC (String c) {
      this.c = c;
    }

    public void setCorrect (String correct) {
        this.correct = correct;
    }

    String[] arr = new String[5];

    arr[0] = question;
    arr[1] = a;
    arr[2] = b;
    arr[3] = c;
    arr[4] = correct;

    System.out.println(arr[0] + "\n");
    System.out.println("Answer a: " + arr[1]);
    System.out.println("Answer b: " + arr[2]);
    System.out.println("Answer c: " + arr[3] + "\n");

    int i = 0;
    while (i != 1) {
        Scanner userin = new Scanner(System.in);

        System.out.println("Type a,b,c for correct answer.\n");

        String selected = userin.next();

        if (selected.equals(correct)) {
            i = 1;
            System.out.println("\nCorrect!");
        }else {
            System.out.println("Try again");
        }
    }
}
}

When i try to create the Array i get the error "Syntax error on token ";", { expected after this token".
I'm relative new to Java.
Thanks in advance

A:

The following piece of code in your class is outside of any method or constructor:
String[] arr = new String[5];

arr[0] = question;
arr[1] = a;
arr[2] = b;
arr[3] = c;
arr[4] = correct;

System.out.println(arr[0] + "\n");
System.out.println("Answer a: " + arr[1]);
System.out.println("Answer b: " + arr[2]);
System.out.println("Answer c: " + arr[3] + "\n");

int i = 0;
while (i != 1) {
    Scanner userin = new Scanner(System.in);

    System.out.println("Type a,b,c for correct answer.\n");

    String selected = userin.next();

    if (selected.equals(correct)) {
        i = 1;
        System.out.println("\nCorrect!");
    }else {
        System.out.println("Try again");
    }
}

This causes an error to occur because every statement must be enclosed in a method or constructor in Java.
Let's try putting this code in a method:
public void ask() {
    String[] arr = new String[5];

    arr[0] = question;
    arr[1] = a;
    arr[2] = b;
    arr[3] = c;
    arr[4] = correct;

    System.out.println(arr[0] + "\n");
    System.out.println("Answer a: " + arr[1]);
    System.out.println("Answer b: " + arr[2]);
    System.out.println("Answer c: " + arr[3] + "\n");

    int i = 0;
    while (i != 1) {
        Scanner userin = new Scanner(System.in);

        System.out.println("Type a,b,c for correct answer.\n");

        String selected = userin.next();

        if (selected.equals(correct)) {
            i = 1;
            System.out.println("\nCorrect!");
        }else {
            System.out.println("Try again");
        }
    }
}

Now, whenever ask is called, the code will be executed.
Here is how you can use your MultipleChoice class in the main method:
MultipleChoice mc = new MultipleChoice("How many months are there in a year?");
mc.setA("11");
mc.setB("12");
mc.setC("13");
mc.setCorrect("b");
mc.ask();

