Search code examples
javajava.util.scanner

Scanner.nextLine() is returning nothing when I try to use it twice


I am making an 8 ball program for my class final and I keep running into errors with the nextLine() function I am using. At first, without the input for the user, it had been printing the answer before any question was answered, and I had thought I'd figured it out, but then it returned nothing when I put them in the same for() loop. What am I doing wrong here? Here is my code.

import java.util.Random;
import java.util.Scanner;
import java.io.*;
class Main {
  public static void main(String[] args) throws IOException {
    Scanner reader = new Scanner(new File("8ball.txt"));
    Scanner reader2 = new Scanner(new File("medievalball.txt"));
    System.out.println("Welcome to 8 ball! You can choose a theme: Medieval or Normal, or type 'quit' to quit at any time.");
    Scanner input = new Scanner(System.in);
    String inInput = input.nextLine();
    String[] normal = new String[12];
    String[] medieval = new String[10];
    Random random = new Random();
    int randomNum = random.nextInt(10)+1;
    Random random2 = new Random();
    int randomNum2 = random2.nextInt(12)+1;
    while (true)
    {
    if (inInput.equals("Normal"))
    {
      System.out.println("You've selected Normal. Preparing... ");



      System.out.println("Ask me something, I can answer.");
      Scanner normalInput = new Scanner(System.in);

      for (int i=0; i < normal.length;i++)
      {
        normal[i] = normalInput.nextLine();
      }
    for (int i=0; i < normal.length;)
    {
      normal[i] = reader.nextLine();
     System.out.println(normal[randomNum]);
     break;
    }

    }

    else if (inInput.equals("Medieval"))
    {
      System.out.println ("You hath selected Medieval. Preparing...");
      System.out.println("I have unbearable knowledge of the world, ask me thine question.");
      Scanner medievalInput = new Scanner(System.in);
      for (int i=0; i < medieval.length;i++)
      {
        medieval[i] = medievalInput.nextLine();
      }
      for (int i=0; i < medieval.length;i++)
      {
        System.out.println(medieval[randomNum2]);
        break;
      }
    }
    else if (inInput.equals ("quit"))
    {
      System.out.println ("See you next time!");
      System.exit(0);
    }
    else
    {
      System.out.println("Sorry, that's not a choice.");
    }
  }
  }
}

Solution

  • You need to build a good understanding of basics before you try to work on this problem. I've highlighted some of the problems from your program:

    1. If the value of normal.length >= 1, the following statement will create an infinite loop. You need to put expression to change the value i (e.g. i++) in the last section of the loop statement.

      for (int i=0; i < normal.length;)
      
    2. You do not need multiple instances of Random to generate more than one random numbers. Given below is an example of how to generate multiple random numbers using the same instance of Random.

      Random random = new Random();
      int randomNum = random.nextInt(10) + 1;
      int randomNum2 = random.nextInt(12) + 1;
      
    3. Whenever you read something from a file using a scanner, you should always use Scanner::hasNextLine to avoid NoSuchElementException e.g. the following code block

      for (int i=0; i < normal.length; i++) {
          normal[i] = reader.nextLine();
          //...
      }
      

      should be written as

      for (int i=0; i < normal.length && reader.hasNextLine(); i++) {
          normal[i] = reader.nextLine();
          //...
      }
      
    4. The break without a condition is meaningless in a loop e.g. the break statement in the following block will cause the loop to terminate after the first iteration refuting the purpose of using the loop.

      for (int i=0; i < medieval.length;i++) {
          System.out.println(medieval[randomNum2]);
          break;
      }