1

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



    }

}
Stidgeon
  • 2,622
  • 8
  • 18
  • 27
reyna
  • 21
  • 2
  • What is your answer for the last question? if it is "y" then it will continue else it won't continue with while loop – Dharmesh Dhameliya Apr 07 '20 at 03:26
  • You need to try debugging your code. While using a real debugger is very helpful, I would start out by printing the value of `answer` at the bottom of the loop after you call `nextLine()`, then reading [the documentation for the `next()` method](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#next--). – chrylis -cautiouslyoptimistic- Apr 07 '20 at 03:30
  • reyna - Any update? – Arvind Kumar Avinash Apr 07 '20 at 03:53
  • If I change the final scan.next() to scan.nextLine(), that worked. This is why I normally use BufferedReader() and parse the numbers myself. – NomadMaker Apr 07 '20 at 04:08
  • yes, it's working now thank you! You helped me so much with my grade in college!! – reyna Apr 07 '20 at 04:27

2 Answers2

0

Using next() will only return what comes before the delimiter (defaults to whitespace). nextLine() automatically moves the scanner down after returning the current line.

To get rid of the \n use scan.nextLine

// gets rid of \n in the input stream
            scan.nextLine();
            answer=scan.nextLine();

Hope this help.

backdoor
  • 776
  • 1
  • 3
  • 15
0

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.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72