0

can i ask whats the problem in my code? its done but the part of the yes or no which is this System.out.println("A refrigerator costs $2.50 extra each day would you like that? (Y/N)"); will be skipped and insted the System.out.println("Number of Days Staying :" + dys); will be printed.

import java.util.Scanner;
public class Test
{
public static void main (String[] args)
{
 Scanner scan = new Scanner(System.in);
 String rt;
 String a = new String ("G");
 String b = new String ("P");
 String c = new String ("L");
 
 System.out.println("  Welcome to Easy Living Resort Hotel");
 System.out.println("------------------------------------------");
 System.out.println("  Room Type                Daily Rate  ");
 System.out.println("  ~~~~~~~~~                ~~~~~~~~~~  ");
 System.out.println(" G- Garden Pool View         $125.00   ");
 System.out.println(" P- Pool View                $145.00   ");
 System.out.println(" L- Lake View                $180.00   ");
 System.out.println("Please select your room type");
 rt = scan.nextLine();
 
   if(rt.equalsIgnoreCase("G")) {
     System.out.println("Garden Pool View");
   } else if(rt.equalsIgnoreCase("P")) {
     System.out.println("Pool View");
    } else {
      System.out.println("Lake view");
 }
 
 int dys;
 System.out.println("------------------------------------------");
 System.out.println("Number of Days you are staying in: ");
 dys = scan.nextInt();
 
 String r;
 String d = new String ("Y");
 String e = new String ("N");
 System.out.println("A refrigerator costs $2.50 extra each day would you like that? (Y/N)");
 r = scan.nextLine();
 
 if(r.equalsIgnoreCase("Y")) {
   System.out.println("Y");
  }else if (r.equals("N")) {
   System.out.println("N");
 }
   
 System.out.println("Number of Days Staying                 :" + dys);
 //System.out.println("
 }
}
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Bernard
  • 17
  • 3
  • Directly after using `dys = scan.nextInt();` you need to consume the rest of the line `scan.nextLine();` otherwise the next call to the scanner will just grab the end of the line that the integer was on (blank, or whatever was typed after a space) instead of waiting for a new input. – sorifiend Oct 18 '21 at 04:37

2 Answers2

0

Here is a working example:

The problem was a missing scan.nextLine(). Otherwise it will take the \n of your previous input and skip the input. I also cleaned up your code. It is better to split up your code in methods.

import java.util.Scanner;
public class Reservation
{
    public static void main (String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String rt;
        String a = "G";
        String b = "P";
        String c = "L";

        showRoomPrices();
        rt = scan.nextLine();

        evaluateEnteredRoomType(rt);

        int dys;
        System.out.println("------------------------------------------");
        System.out.println("Number of Days you are staying in: ");
        dys = scan.nextInt();

        String r;
        String d = "Y";
        String e = "N";
        scan.nextLine();
        System.out.println("A refrigerator costs $2.50 extra each day would you like that? (Y/N)");
        r = scan.nextLine();

        if(r.equalsIgnoreCase("Y")) {
            System.out.println("Y");
        }else if (r.equals("N")) {
            System.out.println("N");
        }

        System.out.println("Number of Days Staying                 :" + dys);
        //System.out.println("
    }

    private static void evaluateEnteredRoomType(final String rt) {
        if(rt.equalsIgnoreCase("G")) {
            System.out.println("Garden Pool View");
        } else if(rt.equalsIgnoreCase("P")) {
            System.out.println("Pool View");
        } else {
            System.out.println("Lake view");
        }
    }

    private static void showRoomPrices() {
        System.out.println("  Welcome to Easy Living Resort Hotel");
        System.out.println("------------------------------------------");
        System.out.println("  Room Type                Daily Rate  ");
        System.out.println("  ~~~~~~~~~                ~~~~~~~~~~  ");
        System.out.println(" G- Garden Pool View         $125.00   ");
        System.out.println(" P- Pool View                $145.00   ");
        System.out.println(" L- Lake View                $180.00   ");
        System.out.println("Please select your room type");
    }
}
SamTV
  • 147
  • 1
  • 11