Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have created the basic structure to the program, but I cant seem to exit the loop no matter what I try.
Also I want it to display the information that the user will input; from the keyboard, on to the console once I have exited the loop, but I am not sure as to how to do that.
import java.util.Scanner;
public class Requirement1 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String name, game, time, points;
int i = 0;
System.out.println("Please Enter Your Name");
name = scan.nextLine();
System.out.println("Players Name: " + name);
while (i < 100)
{
Scanner scan2 = new Scanner(System.in);
System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
game = scan.nextLine();
System.out.println("Game: " + game);
Scanner scan3 = new Scanner (System.in);
System.out.println("Please Enter Your Score");
points = scan.nextLine();
System.out.println("Your Score: " + points);
Scanner scan4= new Scanner (System.in);
System.out.println("Please Enter the amount of time Spent Playing in Minutes");
time = scan.nextLine();
System.out.println("Time played: " + time);
}
}
Include an i++ inside of the loop.
In order to break out of the while loop, you need to either make i < 100 false, or add an explicit statement to stop the loop (break, return, throw or System.exit, depending upon your requirements).
If you want to break the loop when the user enters "quit" and execute the code following the loop:
System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
game = scan.nextLine();
if (game.equals("quit")) {
break;
}
At present, i is never changed from 0, so i < 100 is always true. Also, it is not read otherwise. Unless you need it for some other purpose, you can remove i and change the while loop declaration to:
while (true) {
// ...
}
This condition obviously can never be false, so you would need an explicit break in the loop.
In general, I like to use a for loop when I know the exact number of times the loop will run. If I don't know how many times the loop will run, then I like to use a while loop.
A for loop:
import java.util.Scanner;
public class Requirement1 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String name, game, time, points;
System.out.println("Please Enter Your Name");
name = scan.nextLine();
System.out.println("Players Name: " + name);
for (int i = 0; i < 100; i++) {
Scanner scan2 = new Scanner(System.in);
System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
game = scan.nextLine();
if (game.equals("quit")) {
break;
}
System.out.println("Game: " + game);
Scanner scan3 = new Scanner (System.in);
System.out.println("Please Enter Your Score");
points = scan.nextLine();
System.out.println("Your Score: " + points);
Scanner scan4= new Scanner (System.in);
System.out.println("Please Enter the amount of time Spent Playing in Minutes");
time = scan.nextLine();
System.out.println("Time played: " + time);
}
}
A while loop:
import java.util.Scanner;
public class Requirement1 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String name, game, time, points;
System.out.println("Please Enter Your Name");
name = scan.nextLine();
System.out.println("Players Name: " + name);
while (true) {
Scanner scan2 = new Scanner(System.in);
System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
game = scan.nextLine();
if (game.equals("quit")) {
break;
}
System.out.println("Game: " + game);
Scanner scan3 = new Scanner (System.in);
System.out.println("Please Enter Your Score");
points = scan.nextLine();
System.out.println("Your Score: " + points);
Scanner scan4= new Scanner (System.in);
System.out.println("Please Enter the amount of time Spent Playing in Minutes");
time = scan.nextLine();
System.out.println("Time played: " + time);
}
}
Another alternative is to make use of the for loop, and is more correct for your needs:
for (int i = 0; i < 100; i++) {
//your code
}
Your loop being a simple counter, you might want to replace it with a for-loop as it indicates better that the loop does not break because of some operations happening within the loop:
for(int i = 0 ; i < 100 ; i++) {
// block running 100 times
}
If possible avoid using do / while in favour of a for loop. If the game loop should end when the user types quit, add a break statement.
for (i = 0; i < 100; i++) {
// get user's game choice from console
if (game.equals("quit")){
break;
}
// if they didn't type quit ...
}
Related
I am making a program that will take a user's input on how many numbers he wants and determine the highest number between the given. After that the user will be prompt with a Yes or no question. If the user decides to say yes, the program will loop again and if not, the program will end. Now my question is why does it take the highest number from the previous run?
import java.util.Scanner;
public class IT_VILLAFLOR_Lab1_Prog2
{
public static void main(String[] Args){
int num=1,num2,Largest=0,max;
char YN;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Max Number = ");
max = sc.nextInt();
for(num=1; num<=max; num++)
{
System.out.print("Enter Number " + num + ": ");
num2 = sc.nextInt();
if(Largest<num2)
{
Largest=num2;
}
else if(num==max)
{
System.out.println("The Biggest number is " + Largest );
System.out.print( "Do you want to try again? Y/N ");
YN = sc.next().charAt(0);
if(YN =='Y'|| YN =='y')
{
num=0;
System.out.print('\f');
System.out.print("Enter the Max Number " );
max = sc.nextInt();
}
else
{
System.exit(0);
}
}
}
}
}
If the user wants to continue, you are resetting num to 0. Along with this, Largest also needs to be reset to 0.
num=0;
Largest=0; //new code
By the way, you need to change the line else if(num==max) to if(num==max) . Try the test case with max of 2 and values as 12 ,23.
So I have a program that takes orders for vehicle purchases (meant to learn the basics of java. Right now this iteration of the assignment is focusing on inheritance). Hierarchy: Orders.java (Main program), Vehicle.java (parent class), Boat.java/Car.java/Truck.java(child classes).
I was showing my menus manually in the main program before but tried to delegate that responsibility to my Vehicle class so it would be more generic and each child could pass in a Question Prompt and Array of Choices to the showMenu function. So nothing was being done manually anymore.
Here's what happened when it was done manually (worked fine):
System.out.println("What type of Car is this?");
System.out.println("\t1. Sedan");
System.out.println("\t2. Coupe");
System.out.println("\t3. Wagon");
System.out.print("Choice: ");
while (!sc.hasNext("[123]")) {
System.out.println("");
System.out.println("");
System.out.println("That's not an option! Please try again.");
System.out.println("What type of Car is this?");
System.out.println("\t1. Sedan");
System.out.println("\t2. Coupe");
System.out.println("\t3. Wagon");
System.out.print("Choice: ");
sc.next();
}
choice = sc.next();
if(choice.equals("1")){
car.setCarType("Sedan");
} else if (choice.equals("2")){
car.setCarType("Coupe");
} else if (choice.equals("3")){
car.setCarType("Wagon");
} else{
car.setCarType("unknown");
}
Here's how it works now (crashes before stopping to let the user give input):
public int showMenu(String prompt, String[] choices){
Scanner sc = new Scanner(System.in);
System.out.println(prompt);
String choiceIdentifiers = "";
int choice;
for(int i = 0; i < choices.length; i++){
Integer j = i+1;
System.out.println("\t\t" + j + ". " + choices[i]);
choiceIdentifiers = choiceIdentifiers + j.toString();
}
System.out.print("Choice: ");
while (!sc.hasNext("[" + choiceIdentifiers +"]")) {
System.out.println("");
System.out.println("");
System.out.println("That's not an option! Please try again.");
System.out.println(prompt);
for(int i = 0; i < choices.length; i++){
System.out.println("\t\t" + (i+1) + ". " + choices[i]);
}
System.out.print("Choice: ");
sc.next();
}
choice = Integer.parseInt(sc.next());
sc.close();
return choice - 1;
}
Also here's a link to my github repo with the full program.
What could be happening in the showMenu function to crash the program and throw the NoSuchElementException?
Any help would be greatly appreciated.
The problem is that you're closing the System.in stream when you call sc.close().
When you construct a new Scanner that uses the closed System.in stream and you call next() on it, you'll get this error.
To solve the problem, don't close the System.in (don't call close() on any Scanner that uses this stream) until you're done processing user input.
Here is a MCVE:
public class Example {
public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in);
sc1.close();
Scanner sc2 = new Scanner(System.in);
sc2.next();
}
}
The problem is in Vechile.java show menu method
Scanner sc = new Scanner(System.in);
System.out.println(prompt);
String choiceIdentifiers = "";
int choice;
for(int i = 0; i < choices.length; i++){
Integer j = i+1;
System.out.println("\t\t" + j + ". " + choices[i]);
choiceIdentifiers = choiceIdentifiers + j.toString();
}
so here after iterating on choices array you are concatinating choiceIdentifiers string that will become 12 after loop but you are expecting input from user as 1 or 2 for model type of vechile so if you will put 1 for model then
!sc.hasNext("[" + choiceIdentifiers +"]")
is not having 1 so it will throw no such element error , hope this will help you
package cst150zzhw4_worst;
import java.util.Scanner;
public class CST150zzHW4_worst {
public static void main(String[] args) {
//Initialize Variables
double length; // length of room
double width; // Width of room
double price_per_sqyd; // Total carpet needed price
double price_for_padding; // Price for padding
double price_for_installation; // Price for installation
String input; // User's input to stop or reset program
double final_price; // The actual final price
boolean repeat = true;
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
while (repeat)
{
//User Input
System.out.println("\n" +"What is the length of the room?: ");
length = keyboard.nextInt();
System.out.println("What is the width of the room?: ");
width = keyboard.nextInt();
System.out.println("What is the price of the carpet per square yard?: ");
price_per_sqyd = keyboard.nextDouble();
System.out.println("What is the price for the padding?: ");
price_for_padding = keyboard.nextDouble();
System.out.println("What is the price of the installation?: ");
price_for_installation = keyboard.nextDouble();
final_price = (price_for_padding + price_for_installation + price_per_sqyd)*((width*length)/9);
keyboard.nextLine(); //Skip the newline
System.out.println("The possible total price to install the carpet will be $" + final_price + "\n" + "Type 'yes' or 'no' if this is correct: ");
input = keyboard.nextLine();
}
}
}
How would I make it so when the user says yes the program stop and if the user says no then the program just repeats? I don't know why I'm having so much trouble. I've searched for well over 4 hours. I am only supposed to use a while loop, I think.
You have to assign repeat in your while-loop so it becomes false if the user says yes:
repeat = !input.equalsIgnoreCase("yes");
You just need to set repeat to true or false based on user input. So in the end, compare input with yes or no. Something like this would work for you :
if ("yes".equals(input))
repeat = true; // This would continue the loop
else
repeat = false; // This would break the infinite while loop
boolean repeat = true;
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
while (repeat)
{
-----------------------
-------------------------
System.out.println("Do you want to continue:");
repeat = keyboard.nextBoolean();
}
you also if you want your code to be more systematic , go and search about the interrupt , specially thread interrupt , these answers above is correct , find the more organic code and implement it
You can use a break statement to exit a while loop.
while (...) {
input = ...;
if (input.equals("Y")) {
break;
}
}
My full code is pasted here. Below are the 2 methods that are related to my question.
private static boolean playGameAgain()
{
char playAgain = 'y';
Scanner input = new Scanner(System.in);
while(playAgain != 'n')
{
System.out.println("Go again?(y/n)");
/// PROBLEM HERE.... Somehow, it will NOT wait for INPUT. It just throws an error
playAgain = input.next().charAt(0);
}
input.close();
return (playAgain != 'y')?false:true;
}
// [Trimmed]
public static void initialize(ArrayList<String> names, ArrayList<Integer> scores)
{
Scanner input = new Scanner(System.in);
for (int i = 1; i <= 5; i++)
{
System.out.println("Enter the name for score #" + i + ":");
names.add(input.nextLine());
System.out.println();
System.out.println("Enter the score for score #" + i + ":");
while(!input.hasNextInt())
{
//input.nextLine();
System.out.println("Please input a valid integer value for the score for #" + i + ":");
if(!input.hasNextInt())
input.nextLine();
}
scores.add(input.nextInt());
input.nextLine();
}
input.close();
}
I have tried many combinations of how to read in one character. This used to work, that I could read in one character by using:
Scanner input = new Scanner(System.in);
char temp = input.next().charAt(0);
But, somehow, in this program I wrote, it won't work.
It's a program that will read in 5 user names (strings) & 5 scores (integers) and put them in 2 arrays. It will sort the arrays in descending order and then it will print them out. So, the only problem I have is asking if they want to play again and taking some char input to see if they want to play again (y/n)?
Please help if you can. I've tried many combinations of: if (input.hasNext()), to no avail.
You are not setting playAgain to something other than 'y' in playGameAgain(). You have a bug here.
I am having trouble coding a hw program that is made to generate test with multiple choice and essay questions. Everything works except my program skips lines when it goes to read a part of the essay class. I know it has to do with the scanner and scan.nextline, scan.nextInt and scan.next, etc but I am confused on how exactly to fix it.
Thank you for your help.
import java.util.*;
public class TestWriter
{
public static void main (String [] args)
{
Scanner scan = new Scanner (System.in);
String type=null;
System.out.println ("How many questions are on your test?");
int num = scan.nextInt ();
Question [] test = new Question [num];
for (int i=0; i <num; i++)
{
System.out.println ("Question " + (i+1) + ": Essay or multiple choice question? (e/m)");
type = scan.next ();
scan.nextLine ();
if (type.equals ("e"))
{
test [i] = new Essay ();
test [i].readQuestion ();
}
if (type.equals ("m"))
{
test [i] = new MultChoice ();
test [i].readQuestion ();
}
}
for (int i=0; i <num; i++)
{
System.out.println ("Question " + (i+1)+": "+ type);
test [i].print ();
}
}
}
here is the essay class
public class Essay extends Question
{
String question;
int line;
public void readQuestion ()
{
System.out.println ("How many lines?");
line = scan.nextInt ();
scan.next ();
System.out.println ("Enter the question");
question = scan.nextLine ();
}
public void print ()
{
System.out.println (question);
for (int i=0; i <line; i++)
System.out.println ("");
}
}
Using scan.nextInt() will generate the following problems
If your input is "5 5", nextInt() will get the next integer leaving the remaining " 5" of the buffer line. Of which the remaining " 5" will be caught by
type = scan.next();
In the class test writer:
System.out.println("How many questions are on your test?");
int num = scan.nextInt();
Question[] test = new Question[num]; for(int i=0; i<num; i++)
{
System.out.println("Question " + (i+1) + ": Essay or multiple choice question? (e/m)");
type = scan.next();
This will generate the issue as i have mentioned above.
To fix this you can either
a) Ensure that input is solely a number
b) Get the entire line like so String temp = scan.nextLine(); then convert it to a integer. This will you can play with the string and check if its the input you require i.e if the 1st letter / set of numerical digits is an e/m or an integer.
The problem with scan.nextInt() is that it only gets the next integer of the input line. If there are spaces after the input it was taken from i.e "5 5" it will grab only the next int 5 and leave " 5" behind.
Thus i would recommend using scan.nextLine() and manipulating the string to ensure that the input can be handled and verified and at the same time ensuring that you do not get confused of where the scanner is at.
You should use .next() / .nextInt() if you are handling an input with various parameters you want to specifically catch such as "25 Male Student 1234" in this case the code would be as such
int age = scan.nextInt();
String sex = scan.next();
String job = scan.next();
int score = scan.nextInt();
Your readQuestion function should be ...
public void readQuestion()
{
System.out.println("How many lines?");
line = scan.nextInt();
scan.nextLine();
System.out.println("Enter the question");
question = scan.nextLine();
}
It should be scan.nextLine(); to add an empty new line at the end
In your TestWriter.main() method what are you expecting at 3 line in following code:
System.out.println("Question " + (i+1) + ": Essay or multiple choice question? (e/m)");
type = scan.next();
scan.nextLine(); //LINE 3: What are you expecting user to enter over here.
the control flow will stuck at this point unless you enter something on the console.