-2

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

Florian S.
  • 386
  • 1
  • 12
som3k
  • 57
  • 7
  • 1
    Can you please delete all of that code except for the code where the error is (and maybe a few lines on either side)? Please see: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Joe C Jun 25 '17 at 19:31
  • 1
    And also, please format your code. That might be enough to show you the answer. – Joe C Jun 25 '17 at 19:32
  • 2
    Code formatted. The error is quite simple. Half your code is outside a function. – cs95 Jun 25 '17 at 19:32
  • you should format the code, it will help you to find the error – ΦXocę 웃 Пepeúpa ツ Jun 25 '17 at 19:38
  • Side note: `arr` can be declared as `String[] arr = { question, a, b, c, correct };`. But it looks like you should define a `Question` class instead, because the question, the possible answers and the actual answer are semantically distinct, and so shouldn't just be chucked in with each other. (Or just use the variables directly, there's no real need for an array or class). – Andy Turner Jun 25 '17 at 19:44

2 Answers2

0

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();
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you very much for the precise answer. I learned somthing here :) in the future i will take care of posting clean code. – som3k Jun 25 '17 at 19:48
  • @som3k If you think my answer answers your question, please consider accepting it by clicking on that checkmark! – Sweeper Jun 25 '17 at 19:50
-1

The problem is that you have executable statements in the main body of the class; these executable statements should only be contained in the body of a method. See Syntax error on token ";", { expected after this token in Random string creator

For example, the calls to System.out.println need to be inside a method on this class that can be called elsewhere.

N Shumway
  • 63
  • 1
  • 7