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 3 of 3

Thread: Need help with this nested loop

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with this nested loop

    This is a piece of code from a project I'm working on that prompts the user to pick a number of students between 1 and 10, and a score between 1 and 100 for each of the n number of students. If the user inputs a number of students or a score that is out of the range, an error message pops up and they will be prompted to input this/these bad value(s) again.

    Picking the number of students is simple enough, and the code here works perfectly fine, mostly because it doesn't involve a loop to check the value (only 1 int).

    The "scores" portion of the code (beginning with scores = new double[]) must have a boolean named errFlag that controls the while loop, an inner for loop, and if/else if statements. This code works as well, but the inner "for" loop causes the error in the "else if" statement:

    System.out.println("ERROR: Enter number(s) in the range of 0-100!");
    System.out.println("ERROR: Enter all " + (numStudents) + " scores again: ");

    to print as many times as numStudents (ie 2 students inputted, error lines print twice). I only want this error to print once if any of the values inputted are out of bounds (1-100). Is there any way I can control the loop to only print this error out once no matter how many times the loop executes?



    boolean errFlag = false;
    final int MIN = 0;
    final int MAX = 100;
    final int MAX_STUDENTS = 10;
     
    do{
     //prompt user for number of students between 1 and 10
     System.out.print("Enter number of students(");
     System.out.println((MIN + 1) + "-" + (MAX_STUDENTS + "): "));
     numStudents = scan.nextInt();
     //keep prompting user for number of students until they pick one between 1 and 10
    if(numStudents > MAX_STUDENTS)
     {
       do {
         System.out.println("Error: Enter an integer in the range of 1-10!");
         System.out.print("Enter number of students(");
         System.out.println((MIN + 1) + "-" + (MAX_STUDENTS + "): "));
         numStudents = scan.nextInt();
       }while(numStudents > MAX_STUDENTS);
     }
     
    //prompt the user for scores for each of these students until all five are between 1 and 100
       scores = new double[numStudents];
       System.out.print("Enter " + (numStudents) + " scores(");
       System.out.println((MIN) + "-" + (MAX + "): "));
       while(errFlag = false);
       {
         for(int i = 0; i < numStudents; i++)
             {
           tmp = scan.nextDouble();
           if(tmp >= MIN && tmp <= MAX)
           {
             scores[i] = tmp;
             errFlag = true;
           }
           else if(tmp < MIN || tmp > MAX)
           {
             System.out.println("ERROR: Enter number(s) in the range of 0-100!");
             System.out.println("ERROR: Enter all " + (numStudents) + " scores again: ");
             i--;
             errFlag = false;
           }
         }
       }


  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: Need help with this nested loop

    Is there any way I can control the loop to only print this error out once no matter how many times the loop executes?
    Use a boolean variable. If it is initially true, execute the code to do the print and then set its value false for the next time around the loop.
    boolean oneTime = true;
    ...
    // inside loop
    if(oneTime) {
      oneTime = false;  // turn off for next time
      //  do the one time stuff
    } // end if(oneTime)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with this nested loop

    Sorry for the late response Norm, this did in fact fix the issue I had, and I thank you!

Similar Threads

  1. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum Loops & Control Statements
    Replies: 1
    Last Post: November 24th, 2012, 10:43 AM
  2. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 20th, 2012, 03:49 PM
  3. Nested For Loop!
    By samadniz in forum Object Oriented Programming
    Replies: 3
    Last Post: September 3rd, 2012, 04:03 PM
  4. Nested for loop
    By wolf_fcknHaley33 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 23rd, 2012, 08:49 AM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM

Tags for this Thread