-4
public class decisionMaker {
    public static void main(String args[]) {
        String option[] = new String[10];
        // Output

        for (int i = 0; i <= 9; i++) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the next option:");
            option[i] = input.next();
            System.out.println(" ");
        }

        for (int i = 0; i <= 9; i++) {
            System.out.println("option:   ");
            System.out.println("option[i]+"             ");
        }
    // Output
}

I'm trying to figure out how to add a count to the options, exit and end the program after entering a certain letter or number, and how to create a random output from the user input. I want it to give me one option that I had input at random. Can anyone help me with one or a few of these things. I'm trying to learn to code on my own, and I'm stuck on these.

  • 2
    please add the code in text form to the question and have a look here: [mcve], btw javascript !== java – Nina Scholz Nov 14 '16 at 09:06
  • 2
    it would be good if you post code instead of an image? – Chirag Parmar Nov 14 '16 at 09:06
  • 2
    Please note that *javascript* has absolutely nothing to do with *java*. – charlietfl Nov 14 '16 at 09:08
  • Let start at the begining here, Welcome on SO ! Could you try to explain a bit more your problem. You have asked two questions here I believe. **1.** How to generate a Random number _based on user input_ **2.** How to exit a loop. Could you confirm this ? Both already have answers on SO. **1.** http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range **2.** http://stackoverflow.com/questions/15275195/breaking-out-of-a-for-loop-in-java – AxelH Nov 14 '16 at 11:24

2 Answers2

1

Randomness

You can generate random numbers using java.util.Random;:

import java.util.Random;

public class SomeClass{
    static Random rand = new Random();

    public static void main(String args[]){
        System.out.println(rand.nextInt());
    }
}

About some broken code:

If you want to print out the value of a variable with System.out.println() then you need only type the variable without any quotation marks. The code you've written below will not compile:

System.out.println("option:   ");
System.out.println("option[i]+"             ");

Assuming that's what you want to do, it should instead be written as:

System.out.println("option: ");
System.out.println(option[i]);

Or even System.out.println("option: \n"+option[i]);

(The escape sequence \n when placed inside of quotation marks just indicates to the console to add a new line.)

Scanner:

Additionally, as nick zoum pointed out, your Scanner object should be initialized outside of the for loop, such as right underneath of the main() method.

Please comment below if you need clarification or if I misunderstood what you were looking for. It was very hard to understand your question.

Reason
  • 105
  • 11
  • 2
    Can you add to your answer that OP should move the `Scanner input = new Scanner(System.in);` outside of the loop. – nick zoum Nov 14 '16 at 10:24
  • Good eye @nickzoum. I've edited it to include your help – Reason Nov 14 '16 at 10:32
  • The first part is probably a typo from OP since `"option[i]+" "` i not a a String, this can't compile. And this could also be writen like `System.out.println("option: \n%s", option[i]);` ;) – AxelH Nov 14 '16 at 11:19
  • Correct @AxelH, that's what I'm pointing out in my answer. – Reason Nov 14 '16 at 23:49
  • 1
    The typo is probably not part of the question, this could probably be confirm by the OP. – AxelH Nov 15 '16 at 07:03
  • Good point @AlexH. It may be slightly off-topic for me to have included that in my answer. Since the OP said "*I'm trying to learn to code on my own, and I'm stuck on these*" though, I figured I would try to be as helpful as possible. I've creeped SO for a long time but only recently started getting involved so your judgement is probably better than mine. – Reason Nov 15 '16 at 08:16
0

You could try something like this:

public class DecisionMaker {
    public static void main(String[] args) {
        // output
        Scanner scanner = new Scanner(System.in);
        int size = getInt(scanner);
        String option[] = new String[size];
        for (int index = 0; index < size; index++) {
            System.out.print("Enter the next option:");
            option[index] = scanner.next();
        }
        int index = (int) (Math.random() * size);
        System.out.println(option[index]);
        scanner.close();
        // output
    }

    public static int getInt(Scanner scanner) {
        int size = 0;
        while (size <= 0) {
            if (scanner.hasNext()) {
                if (scanner.hasNextInt()) {
                    size = scanner.nextInt();
                }
            }
            if (size <= 0) {
                System.out.println("The input: " + scanner.next() + " is not a valid value.");
            }
        }
        return size;
    }
}

How the program works:

  • The Scanner is initialized in the beginning and there is only one instance of it.

  • Then the program will wait until the user inserts a valid number for the size of options.

  • The next 5 lines were essentially copied from your code.

  • Finally we get a random Integer in the range of 0 - (size - 1) and print the String of the array with that index.

nick zoum
  • 7,216
  • 7
  • 36
  • 80