-2

I want to allow the user to input a number which will be the number of Strings (choices). The program will then generate a random number that is between 0 and the lenth of the array and display the element that matches the idex of the random number. However whenever I run the program the output simply says "null". I've attached my code. Any advice will help, thanks.

import java.util.Scanner;
import java.util.Arrays;
public class Decision{
    public static void main (String[] args){

        Scanner I= new Scanner(System.in);

        System.out.println("How many choices would you like to choose from?");

        int i=I.nextInt();

        System.out.println("Enter the " + i + " choices:");

        Scanner C= new Scanner(System.in);

        String c= C.nextLine();

        String[] options=new String[i];

        int random = (int)(Math.random() * i +1);

        String choice = options[random];
        System.out.println(choice);

    }
}
progyammer
  • 1,402
  • 3
  • 13
  • 26
Kevin
  • 1
  • 1
  • 1
    No need to make new `Scanner`s on the same stream. Also you're never puttting any values into `options`. (note `c` is never read) – CollinD Dec 19 '16 at 16:11
  • 1
    I don't know who advices the use of Scanners while learning basics. I advice that you first store the inputs in the variables directly instead of reading from input and try to make it work. Use debugger to see how the code works. To make a working program work with real input is then trivial. – Gurwinder Singh Dec 19 '16 at 16:13

2 Answers2

0

Your code has multiple issues:

  • Your console ouput is null, because after creating the array String[] options = new String[i]; you never assign any strings to it, so all elements remain null.
  • You need a loop to retrieve multiple lines with your scanner, C.nextLine(); just reads one line and does not use it anyway.
  • You do not use the java.util.Arrays import.
  • You should not create a new scanner, as you can reuse the first scanner for all input purposes.
  • Please use meaningful names for your variables, consider naming conventions.
  • The random number Math.random() * i + 1) is incorrect for your array indices.

This modified code should help you:

import java.util.Scanner;
import java.util.Random;

public class Decision {

    public static void main (String[] args){

        Scanner scanner = new Scanner(System.in);
        System.out.println("How many choices would you like to choose from?");

        // I use an ArrayList, because it may be hand
        int count = scanner.nextInt();
        String[] choices = new String[count];

        // Necessary, because nextInt() does not consume
        // the newline character.
        scanner.nextLine();

        System.out.println("Enter the " + count + " choices:");

        for (int i = 0; i < count; i++) {
            choices[i] = scanner.nextLine();
        }

        // The method nextInt(int bound) returns an integer
        // in range [0, count - 1].
        Random random = new Random();
        int randomChoice = random.nextInt(count);

        System.out.println(options[randomChoice]);

    }
}

Why Random is a better choice than Math.random() in general was already discussed in this post and the issue with scanners and nextInt() can be found here.

Community
  • 1
  • 1
thatguy
  • 13,242
  • 6
  • 19
  • 33
0

Your code has quite a few problems.

Firstly, calling nextLine after nextInt probably returns "" because nextInt doesn't read the new line character. I suggest you to replace I.nextInt() with Integer.parseInt(I.nextLine()).

Secondly, one scanner is enough. There is no need to create two: I and C. You should also name variables appropriately.

Thirdly, if you want the user to enter a bunch of things, you should use a loop. I think a for loop is suitable for this situation.

Finally, I don't personally like Math.random(). It makes it hard to see what range of random numbers you are generating. I suggest you to use a Random (see Random Numbers part) object instead.

If you're lazy (I hope you are not), here's the code:


    Scanner scan = new Scanner(System.in);

    System.out.println("How many choices would you like to choose from?");

    int numberOfChoices = Integer.parseInt(scan.nextLine());
    String[] options = new String[numberOfChoices];

    for (int i = 0 ; i < numberOfChoices ; i++) {
        System.out.println("Enter choice " + (i + 1) + ":");
        options[i] = scan.nextLine();
    }
    Random rand = new Random();
    int random = rand.nextInt(numberOfChoices);

    String choice = options[random];
    System.out.println(choice);
Anatoly Shamov
  • 2,364
  • 1
  • 13
  • 22
Sweeper
  • 145,870
  • 17
  • 129
  • 225