# Infinite while loop for myKybd

• December 11th, 2012, 04:23 PM
DANGEROUSSCION
Infinite while loop for myKybd
This is the task..............

BirthMonth
This tutorial will use arrays and methods.

Using the material presented in lectures, implement and fully test the BirthMonth program. Use methods to implement at least the following functions:
• input and validate months
• output table of results
• find and output most frequent month

Problems are;

1 = Infinite loop when entering month number
2 = -1 needs to exit program with the results

Thanks for looking

Code Java:

```  import java.util.*;   public class BirthMonth {   public static void main(String[] args) {   int[] MonthTally = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};   inputMonths(MonthTally); outputMonths(MonthTally); findMost(MonthTally); } //select month born public static void inputMonths(int[] MonthTally) {   System.out.println("Enter the number of the month you were born "); Scanner myKeyboard = new Scanner(System.in); int Month = myKeyboard.nextInt() - 1;   while (Month != -1) {   if (Month >= 13) { System.out.println("Enter a number between 1 - 12"); System.out.println("Enter the number of the month you were born "); Month = myKeyboard.nextInt() - 1; } } if (Month >= 1 && Month <= 12) { MonthTally[Month] = MonthTally[Month] + 1; } } //add up month total into array public static void outputMonths(int[] MonthTally) { for (int i = 0; i < MonthTally.length; i++) { System.out.println("Tally: " + i + " has the value " + MonthTally[i]); System.out.println("Tally value " + MonthTally[i]); } } //find highest month public static void findMost(int[] MonthTally) { int largestPos = 0; for (int i = 1; i <= MonthTally.length - 1; i++) { if (MonthTally[i] > MonthTally[largestPos]) { largestPos = i; } } //output results System.out.println("The most Common Month for pregnancy is: " + largestPos); largestPos = largestPos - 1; String[] monthName = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; System.out.print("This is the month of " + monthName[largestPos]); } }```
• December 11th, 2012, 04:39 PM
Norm
Re: Infinite while loop for myKybd
What are the variables that control the loop?
Where does the code change the value in that variable to allow the loop to exit?
You need to change the variable's value to exit the loop.

If you can not see the value of the variable, add a println statement that prints out the value of the variable as the loop executes. The output should show you what the problem is.
• December 11th, 2012, 06:40 PM
DANGEROUSSCION
Re: Infinite while loop for myKybd
where is the solved button on here?
• December 12th, 2012, 02:48 AM
DANGEROUSSCION
Re: Infinite while loop for myKybd
Thanks for your help. Just gotta sort the results out.
• December 12th, 2012, 09:34 AM
Re: Infinite while loop for myKybd
you need to add 2 braces "}" at the end of your programm
• December 12th, 2012, 11:25 AM
copeg
Re: Infinite while loop for myKybd
Quote:

Originally Posted by Mohamad.H
you need to add 2 braces "}" at the end of your programm

Why reply with an incorrect solution to an already solved thread?
• December 13th, 2012, 05:45 AM