-1

The scenario is as follows,

The decorator needs to enter the height of the room (between 2 and 6 metres), then the length of all four walls (minimum 1 metre; maximum 25 metres).

System.out.println("Enter Height of the room");
  Scanner hr = new Scanner(System.in);
     int height = hr.nextInt();

System.out.println("Enter Length1 of the room");
  Scanner l1 = new Scanner(System.in);
         int length = l1.nextInt();

System.out.println("Enter Length2 of the room");
          Scanner l2 = new Scanner(System.in);
             int length2 = l2.nextInt();

System.out.println("Enter Length3 of the room");
          Scanner l3 = new Scanner(System.in);
             int length3 = l3.nextInt();

System.out.println("Enter Length4 of the room");
          Scanner l4 = new Scanner(System.in);
              int length4 = l4.nextInt();

I’ve written the scanners to receive the user’s input, but I don’t how to set a parameter to the scanners. What I want the program to do is receive the user’s input and if (for,example the height of the room is 9 metres) the input is not within the parameters to print an error.

2 Answers2

1

If I understood it right you have to CREATE your so called parameter. The Scanner doesn't do what you want.

So, again, If I understood it right, you should create if conditions to check whether the user gave you the correct input.

And also you just need ONE Scanner instance. So:

Scanner scannerToUsAll = new Scanner(System.in);

System.out.println("Enter Height of the room");
int height = scannerToUsAll.nextInt();

//here you check
if ( height < 2 && height > 6  ){
     System.out.println("The Height is not within the parameters (2 and 6)");
}

If you need to get another input just use the same scanner int length = scannerToUsAll.nextInt();

You will need to control the flow of your application to exit or return to the same question. My tip here: while

Jorge Campos
  • 20,662
  • 7
  • 51
  • 77
0
System.out.println("Enter Height of the room");
  Scanner sc = new Scanner(System.in); 
    int height = sc.nextInt();
    if (height < 2 || height > 6)
    {
        System.out.println("Error: height is invalid");
    }

System.out.println("Enter Length1 of the room");
        int length1 = sc.nextInt();
        if (length1 < 1 || length1 > 25)
        {
            System.out.println("Error: length1 is invalid");
        }

System.out.println("Enter Length2 of the room");
            int length2 = sc.nextInt();
            if (length2 < 1 || length2 > 25)
            {
                System.out.println("Error: length2 is invalid");
            }

... and so on ...

Carlene
  • 237
  • 2
  • 10