I want to do a switch in while loop where at the break of every switch statement the while loop stops and ask for an input like F, R, C, Q. The statement below works but the statement does not break. Please help
public static void main(String[] args) throws IOException {
// start both with 1 point
int goodTotal = 50;
int monTotal = 50;
// input switch statement
while (goodTotal > 0 && monTotal > 0) {
System.out.print("Type a letter: ");
System.out.println("\n");
System.out.print("F: Go out and Fight ");
System.out.println("\n");
System.out.print("R: Rest ");
System.out.println("\n");
System.out.print("C: Check Stats ");
System.out.println("\n");
System.out.print("Q: Quit ");
int input = System.in.read();
System.out.println("You typed: " + (char) input);
switch (input) {
case 'f':
System.out.println("Continue the game");
break;
case 'r':
System.out.println("Players should rest");
break;
case 'c':
System.out.println("Checking the status of the game");
System.out.print("Goodman has " + goodTotal + " points and Monster has " + monTotal + " points");
System.out.println("\n");
break;
case 'q':
System.out.println("Game over");
System.exit(input);
break;
default:
System.out.println("Invalid selection");
break;
}
// Set value of minimum and maximum damage
int minDmg = 2;
int maxDmg = 15;
// Get random number;
int damage = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();
int damage2 = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();
// remove value of damage from started value to get total value remaining
goodTotal = goodTotal - damage;
monTotal = monTotal - damage2;
// print message if still in the game
if (goodTotal > 0) {
System.out.println("Goodman has " + goodTotal + " points left. Not bad, Man! ");
}
// if Goodman survives round 2 print a message of encouragement
if (goodTotal > 0 && count > 1 && count <= 2) {
System.out.print("This is encouraging. Goodman has lasted past roundhh " + count + ". ");
// print new message if Goodman passes round 3
} else if (goodTotal > 0 && count == 3) {
System.out.print("Goodman is as strong as Samson. He has lasted round " + count
+ " and still looks strong.");
System.out.print(" 10 hit points has been added to your total");
}
if (monTotal > 0) {
System.out.println("Wait, Monster has a total of " + monTotal + " points and is still in the game");
}
// exit if have less than 0 point, and print game over. Congratulate the winner
if (goodTotal < 0) {
System.out.println("Goodman you are out of the game");
System.out.println("The monster will take over the village. This is sad");
System.out.println("Game Over!");
} else if (monTotal < 0) {
System.out.println("Goodman has been victorious");
System.out.println("The monster is dead. The people live!!!!");
System.out.println("Game Over!");
}
System.out.println("This is the end of round " + count + " ");
System.out.println("\n");
count = count + 1;
}
}
Use a label on the loop:
loop: while (goodTotal > 0 && monTotal > 0) {
// ...
switch (input) {
case 'f':
// ...
break loop;
case 'r':
// ...
break loop;
// ...
}
// ...
}
You should use labelled breaks.
Although it probably is better to rewrite the code in a way that doesn't need them, as they're not very easy to read.
input is of type int, but the case labels are character literals ( i.e., f,r,c,q). Why don't you just make the input also a char type ?
char input = System.in.read();
Related
I'm just trying to build a calculator and I have no idea how to prompt an operator before each number. I'm trying to build a calculator and have as many number inputs as specified but they can cancel out of it by pressing -1 and it will still return the final value.
But i tried using a string statements but I'm new to coding and don't know where to start.
int operator;
int howmany;
int i=0;
int all = 0;
double div;
int scan;
System.out.println("Press 1 for addition, 2 for subtraction, 3 for \n
multiplication 4 for division or 5 for modules");
operator=scanner.nextInt();
if(operator < 1 || operator >5) {System.out.println("Restart
calculator");System.exit(0);}
System.out.println("How many numbers will you be using?");
howmany = scanner.nextInt() -1 ;
if (operator == 1){
System.out.println("Press -1 to cancel ");
while(i <=howmany) {
i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all += scan;
if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}
if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}
if (operator == 2){
System.out.println("Press -1 to cancel ");
while(i <=howmany) {
i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all -= scan;
if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}
if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}
if (operator == 3){
all = 1;
System.out.println("Press -1 to cancel ");
while(i <=howmany) {
i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all *= scan;
if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}
if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}
if (operator == 4){
System.out.println("Press -1 to cancel ");
while(i <=howmany) {
i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all = scan;
if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}
if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}
if (operator == 5){
System.out.println("Press -1 to cancel ");
while(i <=howmany) {
i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all %= scan;
if(i-1 == howmany ) {
System.out.println("Your final answer is " + all);
}
if(scan == -1) {
System.out.println("No more inputs, final answer was " + all);
break;
}
}
}
}
}
When you don't know where to start when coding, it can help to write the algorithm in sentences first, and then convert into code. For a calculator program for example, I would start with this basic structure:
// need a data structure to hold all numbers
// need a data structure to hold all operations
while(the user wishes to continue) {
while(the user has not hit =) {
// use the scanner to get the next number (if it's -1, exit program)
// use the scanner to get the next operator (if it's = then exit inner
// loop to report result)
}
// need variable to hold calculation result, instantiate to first number
for(loop through numbers data structure) {
// apply next operator to the running result and next number
}
// report result to user
}
Something else to consider: a calculator should be able to use -1 in calculations. I'm not sure if this is a requirement of what you're working on, so I left it as you described, but what would be more in line with the spirit of a calculator would be to do something like ask the user after each successful calculation if they'd like to continue (y/n).
I have a problem with my program.
Program is a HILO game which requires the user to guess a number between a range of a generated number.
The problem is that once i press 0 on my keyboard, it reveals the random number, and essentially must end the loop and ask whether the user wants to continue or end the program. Instead, it continues the loop, and whenever I press a letter on the keyboard it shows me an Exception error which describes and input Miss Match. Can some one guide me on how to fix it?, maybe I wrote the program wrong, I tried multiple ways, it still doesn't work as it supposed to work.
Thank you.
import java.util.Scanner;
import java.util.Random;
import java.lang.Math;
public class HILOGAME
{
public static void firstGame()
{
final int range = 50;
int answer = 0;
int guesses;
int number;
int guessNum = 0;
String choice = "";
boolean Loop = false;
Scanner input = new Scanner(System.in);
Random r = new Random();
answer = r.nextInt(range) + 1;
System.out.println("IF YOU WISH TO GIVE UP, PRESS 0 ON THE KEYBOARD");
while (!Loop)
{
for (int exit = 0; exit < 10; exit++)
{
System.out.print("Guess a number between 1 and " + range + " : ");
number = input.nextInt();
guessNum = guessNum++;
guessNum += 1;
if (number == answer)
{
System.out.println(" Your guess was correct ");
System.out.println();
System.out.println("The number was: " + answer);
System.out.println("You guess the number with: " + guessNum + " guesses ");
System.out.println();
System.out.println("Enter x to continue to play or y to endgame");
choice = input.nextLine();
if (!choice.equalsIgnoreCase("x"))
{
Loop = true;
answer = r.nextInt(range) + 1;
System.out.print("A new number");
break;
}
else if (choice.equalsIgnoreCase("y"))
{
Loop = false;
System.out.print("END OF GAME");
exit = 11;
}
}
if (number > answer)
{
System.out.println("TOO HIGH");
}
if (number < answer)
{
System.out.println("TOO LOW");
}
if (number == 0)
{
System.out.println("YOU GAVE UP. THE NUMBER WAS " + answer);
System.out.println("Enter x to continue to play or y to endgame");
}
}
}
}
}
EDIT 1:
This code is working perfectly for me i have tried running it:
final int range = 50;
int answer = 0;
int guesses;
int number;
int guessNum = 0;
String choice = "";
boolean Loop = false;
Scanner input = new Scanner(System.in);
Random r = new Random();
answer = r.nextInt(range) + 1;
System.out.println("IF YOU WISH TO GIVE UP, PRESS 0 ON THE KEYBOARD");
while (!Loop)
{
int exit;
for (exit = 0; exit < 10; exit++)
{
System.out.print("Guess a number between 1 and " + range + " : ");
number = input.nextInt();
guessNum = guessNum++;
guessNum += 1;
if (number == answer)
{
System.out.println(" Your guess was correct ");
System.out.println();
System.out.println("The number was: " + answer);
System.out.println("You guess the number with: " + guessNum + " guesses ");
System.out.println();
System.out.println("Enter x to continue to play or y to endgame");
choice = input.next();
// HERE YOU ARE DOING THE WRONG THING
// What is the meaning of "not(!)" here ? !choice.equalsIgnoreCase("x")
// Remove "NOT(!)" from here your program will work as expected
if (choice.equalsIgnoreCase("x"))
{
//here you should assign loop to false if they continue
Loop = false;
answer = r.nextInt(range) + 1;
System.out.print("A new number");
break;
}
else if (choice.equalsIgnoreCase("y"))
{
//better to insert break here as well
//here you should assign loop to true if they wanna exit.
Loop = true;
System.out.print("END OF GAME");
exit = 11;
break;
}
}
else if (number > answer)
{
System.out.println("TOO HIGH");
}
else if (number < answer)
{
System.out.println("TOO LOW");
}
if (number == 0)
{
System.out.println("YOU GAVE UP. THE NUMBER WAS " + answer);
System.out.println("Enter x to continue to play or y to endgame");
// MADE CHANGES HERE
choice = input.next();
if (choice.equalsIgnoreCase("x"))
{
//here you should assign loop to false if they continue
Loop = false;
answer = r.nextInt(range) + 1;
System.out.print("A new number");
break;
}
else if (choice.equalsIgnoreCase("y"))
{
//better to insert break here as well
//here you should assign loop to true if they wanna exit.
Loop = true;
System.out.print("END OF GAME");
exit = 11;
break;
}
}
}
}
Please do copy-paste the code. Because it is working perfectly without any Exception..!!
My sample outputs:
SAMPLE 1:
Guess a number between 1 and 50 : 1
TOO LOW
Guess a number between 1 and 50 : 0
TOO LOW
YOU GAVE UP. THE NUMBER WAS 27
Enter x to continue to play or y to endgame
y
END OF GAME
SAMPLE 2:
Guess a number between 1 and 50 : 20
TOO HIGH
Guess a number between 1 and 50 : 19
TOO HIGH
Guess a number between 1 and 50 : 15
Your guess was correct
The number was: 15
You guess the number with: 3 guesses
Enter x to continue to play or y to endgame
x
A new numberGuess a number between 1 and 50 : 20
TOO LOW
Guess a number between 1 and 50 : 0
TOO LOW
YOU GAVE UP. THE NUMBER WAS 42
Enter x to continue to play or y to endgame
y
END OF GAME
Do let me know if there is any problem.
Well, if you wish to end a loop, you could use the break; command. It jumps out of a while or for loop. So you could use this if the number is 0:
if (number == 0)
{
System.out.println("YOU GAVE UP. THE NUMBER WAS " + answer);
System.out.println("Enter 'x' to continue to play or y to endgame");
choice = input.nextLine();
if(choice == "y")
{
break;
}
and add:
if(choice == "y") {break;} to the very end of the while(!Loop) loop.
And with the time when the answer is correct, you set Loop to false, which means !Loop would evaluate to true. You should change Loop = false to Loop = true when they decide to continue the game.
a small modification
public class HILOGAME {
public static void firstGame() {
final int range = 50;
int answer;
int number;
int guessNum = 0;
String choice = "";
Scanner input = new Scanner(System.in);
Random r = new Random();
answer = r.nextInt(range) + 1;
System.out.print("Guess a number between 1 and " + range + " : ");
number = input.nextInt();
if (number == answer) {
System.out.println(" Your guess was correct ");
System.out.println();
System.out.println("The number was: " + answer);
System.out.println("You guess the number with: " + guessNum + " guesses ");
System.out.println();
} else {
System.out.println("you entered a wrong value");
System.out.println("do you really want to try again? press y to continue or other alphabet to exit");
choice = input.next();
playAgain(choice);
}
}
public static void playAgain(String choice) {
if (choice.equalsIgnoreCase("Y")) {
firstGame();
} else {
System.out.println("you chose to quit");
System.exit(0);
}
}
I am working on my final project for my intro to java class and i am having a hard time understanding the errors in my project and why it will not run if you could tell me why i would greatly appreciate it
public static void main(String[] args) {
Scanner scanner1;
int dice, dice2;
int pScore, cScore = 0;
int pTotalScore = 0;
int cTotalScore = 0;
final int maxScore = 750;
String input = "R";
String input2 = "R";
char repeat;
Random randomNumbers = new Random();
System.out.println("Welcome to Our version of the dice game Pig");
System.out.println("Here are the instructions");
System.out.println("On a turn, the player or computer rolls the die repeatedly");
System.out.println("Until either a 1,7,12, or 17 is rolled");
System.out.println("or the player or computer holds");
System.out.println("If a 1,7,12, or 17 is rolled, that player's turn ends");
System.out.println("and no points are earned");
System.out.println("If the player chooses to hold, all of the points rolled during");
System.out.println("that turn are added to his or her score.");
System.out.println("First player to 750 points or more WINS!");
System.out.print("\nPlease enter your name: ");
scanner1 = new Scanner(System.in);
String pName = scanner1.nextLine();
System.out.print("\nI Hope You have fun," + pName);
do { // run at least once. Start of loop
dice = randomNumbers.nextInt(6) + 1;
System.out.println();
System.out.printf("%s you rolled a %d %n", pName, dice);
if (dice == 1 || dice == 7 || dice == 12 || dice == 17) // if these numbers, end
{
pScore = 0;
System.out.println("Turn over.");
System.out.println(" " + pName + " total is " + pScore + " ");
break;
} else { // else ask for re-roll
pScore = dice;
pTotalScore += pScore;
System.out.print(+pScore + " Your turn total is " + pTotalScore + " ");
System.out.print("Enter (R) to roll or (H)to hold: ");
input = scanner1.nextLine();
repeat = input.charAt(0);
}
if (repeat != 'R') { // if something other than R, end
break;
}
} while (pTotalScore < 750 || cTotalScore < 750); // allow repeat so long as scores are less than 750
if (repeat == 'H') {
System.out.println("Turn over.");
System.out.print("Current score: " + pname + " has " + pTotalScore);
System.out.println("The Computer has " + cTotalScore);
break;
}
while (input.equalsIgnoreCase("R"));
if (pTotalScore >= maxScore) {
System.out.println("Your total Score is " + totalScore);
System.out.println(+pname + "WINS!");
break;
}
System.out.println();
System.out.println("It is the Computer's turn.");
do {
dice2 = randomNumbers.nextInt(6) + 1;
System.out.println("The Computer rolled: " + dice2);
if (dice2 == 1 || dice2 == 7 || dice2 == 12 || dice2 == 17) {
cScore = 0;
System.out.print("Turn over");
System.out.println("The Computer total is " + cTotalScore);
break;
} else {
cScore = dice2;
cTotalScore += cScore;
System.out.print("The Computer's total is " + cTotalScore + " ");
System.out.print("Enter (r) to Roll or (H)to Hold: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
}
if (repeat == 'H') {
System.out.println("Turn over");
System.out.print("Current score:" + pName + " has " + pTotalScore);
System.out.println(", The Computer has " + cTotalScore);
break;
}
} while (input2.equalsIgnoreCase("R"));
if (cTotalScore >= maxScore) {
System.out.println("The Computer's score is " + cTotalScore + "\n");
System.out.println("The Computer wins!!!!");
System.out.printl("Run The uprisng has begun!!!!!!");
break;
}
Final3.java:112: error: reached end of file while parsing } ^ 1 error
now the problem is i get the error basically means im missing a } but i cant see where it would be nd no matter where i put it it still says
Final3.java:112: error: reached end of file while parsing } ^ 1 error
You have one while loop that does nothing - while (input.equalsIgnoreCase("R")); - and you didn't close your main method. Add } at the end.
Add a closing brace "}" in the end.I hope this will solve your purpose.
I've made a program that asks the user for a number and then checks if it is equal, less or greater than a random number. A siple guessing game. It works perfectly but I would like to display the output on the same line as the input but I can't figure out how to do this.
Ok, I've picked a number between 1 and 100. What is your first guess?
Guess 1: 36
<
Guess 2: 50
>
Guess 3: 46
That is correct. The hidden number is 46.
This is how I want it to look like:
Guess 1: 36 <
Guess 2: 50 >
Guess 3: 46 That is correct. The hidden number is 46.
How do I do this? The cursor is moving down after the input when I use Scanner.nextInt();
This is my current code:
while (!isCorrect) {
System.out.print("Guess " + guessCount + ": ");
currentGuess = input.nextInt();
if (currentGuess == randomNumber) {
isCorrect = true;
System.out.println("That is correct. The hidden number was " + randomNumber);
}
else if (currentGuess > randomNumber) {
System.out.println(" >");
++guessCount;
}
else if (currentGuess < randomNumber) {
System.out.println(" <");
++guessCount;
}
}
That's not consistently possible. You could try echoing a \b backspace character, but that probably wouldn't work. Once the user types the new line, there is no way to take it off.
The best you are going to be able to accomplish is to re-echo the input.
while (!isCorrect) {
System.out.print("Guess " + guessCount + ": ");
currentGuess = input.nextInt();
if (currentGuess == randomNumber) {
isCorrect = true;
System.out.println("That is correct. The hidden number was " + randomNumber);
}
else if (currentGuess > randomNumber) {
System.out.println(currentGuess + " >");
++guessCount;
}
else if (currentGuess < randomNumber) {
System.out.println(currentGuess + " <");
++guessCount;
}
}
for the output:
Guess 1: 36
36 <
Guess 2: 50
50 >
Guess 3: 46
That is correct. The hidden number is 46.
If you really need to format your output like that you could try saving your input to an arraylist, clearing the screen, then printing all the previous inputs to the screen again.
List<Integer> foo = new ArrayList<Integer>();
while (!isCorrect) {
System.out.print("Guess " + guessCount + ": ");
guessCount++;
currentGuess = input.nextInt();
foo.add(currentGuess);
for(int a = 0; a < 50; a++) {
System.out.println('\n');
}
for(int i = 0; i < foo.size(); i++) {
currentGuess = foo.get(i);
System.out.print("Guess " + (i+1) + ": " + currentGuess);
if (currentGuess == randomNumber) {
isCorrect = true;
System.out.println(" That is correct. The hidden number was " + randomNumber);
}
else if (currentGuess > randomNumber) {
System.out.println(" >");
}
else if (currentGuess < randomNumber) {
System.out.println(" <");
}
}
}
To clear the screen I just printed out 50 blank lines.
A solution to clear the console is proposed here:
Java: Clear the console
however, it didn't work for me or Chris.
I have been working on this programming for about 3 week now and can not figure out my mistakes. I have to use two public classes: 1) validateLength(Number) and 2) convertIntegerToWords(Number). My problem is that once the user inputs their integer my loop continues on forever. The system will ask for an integer, user input, system out either too long or continue on to convertIntgerToWords. My code is below
import java.util.Scanner;
public class Project2 {
public static void main(String [] args) {
//Main Method//
//Create a Scanner//
Scanner input = new Scanner(System.in);
//Enter an Integer//
System.out.print(" What is your integer ? ");
int Number= input.nextInt();
while (Number >= 0) {
if (Number != 0)
validateLength(Number);
else if(Number == 0) {
System.out.print( "Thank you for playing! " + "Good bye! ");
break;
}
}
}
//Next Method//
public static boolean validateLength(int userNum) {
String Number = "" + userNum;
while (userNum >= 0) {
if (userNum < 10)
convertIntegerToWords(userNum);
else if (userNum > 9){
System.out.print("Your integer is too long !");
break;
}
}
}
//End of validate//
//Final Method//
public static String convertIntegerToWords(int Number) {
if (Number == 1)
System.out.println("Your integer " + Number + "is written out as one");
else if (Number == 2)
System.out.println("Your integer " + Number + "is written out as two");
else if (Number == 3)
System.out.println("Your integer " + Number + "is written out as three");
else if (Number == 4)
System.out.println("Your integer " + Number + "is written out as four");
else if (Number == 5)
System.out.println("Your integer " + Number + "is written out as five");
else if (Number == 6)
System.out.println("Your integer " + Number + "is written out as six");
else if (Number == 7)
System.out.println("Your integer " + Number + "is written out as seven");
else if (Number == 8)
System.out.println("Your integer " + Number + "is written out as eight");
else if (Number == 9)
System.out.println("Your integer " + Number + "is written out as nine");
return Number + "";
}
}
}
You need to move
Number = input.nextInt();
inside of the while loop. Here's the typical idiom (other cleanup added as well):
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// Enter an Integer//
System.out.print(" What is your integer ? ");
int Number;
while ((Number = input.nextInt()) >= 0)
{
if (Number == 0)
{
System.out.print("Thank you for playing! " + "Good bye! ");
break;
}
validateLength(Number);
}
}
Edit
if the user enters 0 then yes the program terminates. However if the user enters an integer 1-9, the program should spell out the integer in words (ie 1 is written out as one). It does this but it loops infinite. Same as if the user enters an integer larger than 9 it reports that the "YOur integer is too big, enter another integer" This however, repeats on the same line over and over.
That's because of the while loop in validateLength(). Try this out (note the other code cleanup as well):
public class ScannerDemo
{
private static void convertIntegerToWords(int num)
{
String message = null;
if (num > 9)
{
message = "Your integer is too long!";
}
else if (num > 0)
{
message = "Your integer " + num + " is written out as ";
String numString = "";
switch (num)
{
case 1:
numString = "one"; break;
case 2:
numString = "two"; break;
case 3:
numString = "three"; break;
case 4:
numString = "four"; break;
case 5:
numString = "five"; break;
case 6:
numString = "six"; break;
case 7:
numString = "seven"; break;
case 8:
numString = "eight"; break;
case 9:
numString = "nine"; break;
}
message += numString;
}
System.out.println(message);
}
private static int getNextNumber(Scanner s)
{
System.out.println("What is your integer?");
return s.nextInt();
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int number;
while ((number = getNextNumber(input)) >= 0)
{
if (number == 0)
{
System.out.println("Thank you for playing! Good bye!");
break;
}
convertIntegerToWords(number);
}
}
}
It's also on github.
your while conditional is always satisfied, so it will continue to go through the while loop. It will only stop when Number is no longer greater than or equal to zero.
you need to use some other sort of condition entirely, if you want to use a loop (although I cannot tell why you want to in this example). You are never changing the value associated with Number, so it will always be whatever it was instantiated as. Maybe you meant to have code that changes its value given a certain condition? If not, you need to lose that condition entirely.
while (Number >= 0) {
Is creating a while loop, where inside I do not see you decreasing your Number integer to stop the loop. Do you need to use a loop here? You should try an if statement instead.
Also, you may want to consider a switch statement instead of if for the output.
Welcome to StackOverflow.
I feel need to mention you have one class Project2 and two methods not classes. You get an int not an integer and you fail to read the next line which I suspect is your problem, unless you expect the user to type everything on the first line.
I suggest you learn to use a debugger as this can be very useful in finding debugs and understanding what your program is doing. esp for loops which don't end.
//Main Method//
public static void main(String [] args) {
//Create a Scanner//
Scanner input = new Scanner(System.in);
bool bPlay = true;
while (bPlay) {
//Enter an Integer//
System.out.print(" What is your integer ? ");
int Number= input.nextInt();
if (Number != 0)
validateLength(Number);
else if(Number == 0) {
System.out.print( "Thank you for playing! " + "Good bye! ");
bPlay = false;
break;
}
}
}
public static boolean validateLength(int userNum) {
String Number = "" + userNum;
while (userNum >= 0) {
if (userNum < 10)
convertIntegerToWords(userNum);
else if (userNum > 9){
System.out.print("Your integer is too long !");
break;
}
}
This code is responsible of the problem, you never assign userNum a new value but loop while userNum>0. This make a sweet spot for an infinite loop for number >=0 and <10. (you leave the loop ony for number>9).