My while loop in java doesn't work and only completes it one time

reyna :

It will only go through one time so it will end with "Would you like to make another request?(y/n)" and when I input "y" it stops there and won't do the loop.

package Chaterp5PPReynaGuerra;
import java.util.*;
public class MeetingRequest 
{

    public static void main(String[] args) 
    {

        Scanner scan = new Scanner(System.in);

        final int CAPACITY=30;
        String name;
        int people;
        int morePeople;
        String answer="y";
        int fewerPeople;

        System.out.println("--------Meeting Request System"+
        "--------");

        System.out.println("\nWelcome to the Meeting Request System."+
        " May I know your name?");
        name=scan.nextLine();


        while(answer.equalsIgnoreCase("y")) 
        {
            System.out.println("Hello, "+name+", how many people"+
                    " will be attending the meeting?");
                    people=scan.nextInt();

                    morePeople = CAPACITY - people;
                if(people < CAPACITY)
                    System.out.println("You can invite "+morePeople+
                    " more people to the meeting.");
                else if(people > CAPACITY) {
                    fewerPeople= people - CAPACITY;
                         System.out.println("Sorry, the room is not "+
                "big enough to seat that many people. You have to "+
                     "exclude "+fewerPeople+" from the meeting.");
                    }
            System.out.println();

            System.out.println("Would you like to make another"+
            " request?(y /n)");
            // gets rid of \n in the input stream
            scan.next();
            answer=scan.nextLine();
        }



    }

}
Arvind Kumar Avinash :

Replace

people=scan.nextInt();

with

people = Integer.parseInt(scan.nextLine());

Check Scanner is skipping nextLine() after using next() or nextFoo()? to learn more about it.

Given below is the corrected program:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        final int CAPACITY = 30;
        String name;
        int people = 0;
        int morePeople;
        String answer = "y";
        int fewerPeople;
        boolean valid;
        System.out.println("--------Meeting Request System" + "--------");

        System.out.println("\nWelcome to the Meeting Request System." + " May I know your name?");
        name = scan.nextLine();

        do {
            do {
                valid = true;
                System.out.println("Hello, " + name + ", how many people" + " will be attending the meeting?");
                try {
                    people = Integer.parseInt(scan.nextLine());
                } catch (NumberFormatException e) {
                    System.out.println("Invalid entry. Pleaase try again.");
                    valid = false;
                }
            } while (!valid);

            morePeople = CAPACITY - people;
            if (people < CAPACITY)
                System.out.println("You can invite " + morePeople + " more people to the meeting.");
            else if (people > CAPACITY) {
                fewerPeople = people - CAPACITY;
                System.out.println("Sorry, the room is not " + "big enough to seat that many people. You have to "
                        + "exclude " + fewerPeople + " from the meeting.");
            }
            System.out.println();

            System.out.println("Would you like to make another" + " request?(y /n)");
            answer = scan.nextLine();
        } while (answer.equalsIgnoreCase("y"));
    }
}

A sample run:

--------Meeting Request System--------

Welcome to the Meeting Request System. May I know your name?
abc
Hello, abc, how many people will be attending the meeting?
x
Invalid entry. Pleaase try again.
Hello, abc, how many people will be attending the meeting?
10.4
Invalid entry. Pleaase try again.
Hello, abc, how many people will be attending the meeting?
4
You can invite 26 more people to the meeting.

Would you like to make another request?(y /n)
y
Hello, abc, how many people will be attending the meeting?
5
You can invite 25 more people to the meeting.

Would you like to make another request?(y /n)
n

Some other important points:

  1. As you can see, a do...while loop is more appropriate instead of while loop in this case.
  2. You should always check for the NumberFormatException whenever you parse a text (e.g. Scanner::nextLine()) to integer.

Feel free to comment in case of any doubt/issue.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=477944&siteId=1