Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Infinite while loop for myKybd

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

     
    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]);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    DANGEROUSSCION (December 11th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Infinite while loop for myKybd

    where is the solved button on here?

  5. #4
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Infinite while loop for myKybd

    Thanks for your help. Just gotta sort the results out.

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Infinite while loop for myKybd

    you need to add 2 braces "}" at the end of your programm

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Infinite while loop for myKybd

    Quote Originally Posted by Mohamad.H View Post
    you need to add 2 braces "}" at the end of your programm
    Why reply with an incorrect solution to an already solved thread?

  8. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Infinite while loop for myKybd

    sorry i did not see the braces my bad...

Similar Threads

  1. Infinite loop?
    By jackfletcher in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2012, 01:06 PM
  2. [SOLVED] Infinite loop issue
    By Sharmeen in forum Loops & Control Statements
    Replies: 1
    Last Post: October 20th, 2012, 12:18 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. help me terminate the infinite loop.
    By ab7 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 10th, 2012, 03:40 AM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM