Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming. can anybody check my code. I don't whats wrong with it. import java.io.File; import java.io.FileNotFoundException; import java.util.Random; import java.util.Scanner; public class Magic8Ball {

Java Programming. can anybody check my code. I don't whats wrong with it.

import java.io.File; import java.io.FileNotFoundException; import java.util.Random; import java.util.Scanner;

public class Magic8Ball { static String[] theMessage; public static void main(String args[]) {

String fileName = ("");

if (args.length == 0) { Scanner s = new Scanner(System.in); System.out.print("Please enter file name:"); fileName = s.nextLine(); s.close(); } else if (args.length == 1) { fileName = args[0];

} else { System.out.println("Usafe: java m8b[filename]"); System.exit(0); }

try { Scanner f = new Scanner(new File(fileName)); String s_size = f.nextLine(); int size = Integer.parseInt(s_size); theMessage = new String[size]; for (int i = 0; i

String question = ""; Scanner sc = new Scanner(System.in); Random random = new Random(); do { System.out.print("Your question please? "); question = sc.nextLine();

if (!question.equals("")) { System.out.println(theMessage[random.nextInt(theMessage.length)]); } } while (!question.equals(""));

System.out.println("Bye! Visit me again!"); sc.close(); } }

Error code:

Your question please? Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at test.Magic8Ball.main(Magic8Ball.java:50)

image text in transcribed

Magic 8 Ball Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as "I don't think so", "Yes, of course!", "I'm not sure", and so forth. The program should read the responses from the file into a list. It should prompt the user to ask a question, then display one of the responses, randomly selected from the list. The program should repeat until the user is ready to quit. Contents of 8_ball_responses.txt: Yes, of course! without a doubt, yes. You can count on it. For sure! Ask me later. I'm not sure. I can't tell you right now. I'll tell you after my nap. No way! I don't think so. without a doubt, no. The answer is clearly NO. Magic 8 Ball Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as "I don't think so", "Yes, of course!", "I'm not sure", and so forth. The program should read the responses from the file into a list. It should prompt the user to ask a question, then display one of the responses, randomly selected from the list. The program should repeat until the user is ready to quit. Contents of 8_ball_responses.txt: Yes, of course! without a doubt, yes. You can count on it. For sure! Ask me later. I'm not sure. I can't tell you right now. I'll tell you after my nap. No way! I don't think so. without a doubt, no. The answer is clearly NO

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions