0

For school, we have to complete the following assignment;

"Code a program that draws randomly from a string array of 10 open-ended questions, whose answer is typed to the console by the user and stored in an "output.txt" file with questions before it."

import java.util.Scanner;

public class Questions {

public static void main(String args[]) {
  int random = (int)(Math.random()*10);
  int length;
  Scanner scanner = new Scanner(System.in);
  Scanner keyboard = new Scanner(System.in);
  int answer;

  String question[] = new String[11];

  question[0] = "What is your name?";
  question[1] = "When is your birthday?";
  question[2] = "What color are your shoes?";
  question[3] = "How are you feeling today? ";
  question[4] = "How old are you?";
  question[5] = "What is your favorite color?";
  question[6] = "How many siblings do you have?";
  question[7] = "How many pets do you have?";
  question[8] = "What is the weather like today?";
  question[9] = "How tall are you?";
  question[10] = "What is today's date?";

 System.out.println(question[random]);
  answer = scanner.nextInt();

   }
}

This is what I have so far, but I have 2 issues,

When I execute the program, type in an answer to a randomly generated question, and hit enter, an error show up (java.util.InputMismatchException)

I don't really know how to make the program store the user input and the questions in a separate file (I only just started coding a few weeks ago)

Any help would be greatly appreciated. Thank you in advance!

1 Answers1

0

Consider:

   String answers[] = new String[ question.length() ];
   ...
   answers[random] = scanner.nextLine();
FredK
  • 4,036
  • 1
  • 6
  • 11
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. – Sagar Chauhan Feb 27 '19 at 06:28