1

Okay so, I have this piece of code:

if (type == 1){
            System.out.println("Agency name: ");
            agencyName = in.nextLine();


            System.out.println("No. of days: ");
            days = in.nextInt();


            Patuvanje p = new PraznicnoPatuvanje (agencyName, 20, days);
            nizaPatuvanje[i] = p;
        }

The scanner is reading 'agencyName' and 'days' without me trying to input the agencyName at first. What code should I add so that the compiler would wait until I initialize the agencyName variable, and then let me assign the 'days' variable.

This is how it looks like in the Console:

enter image description here

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50

2 Answers2

0

In the code preceding this, you are reading the number of vacations

After doing this you are pressing Enter This needs to be consumed too.

Change your code to

in.nextLine (); // comse previouslt entered `Enter`
System.out.println("Agency name: ");
agencyName = in.nextLine();
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0
   if (type == 1){
                    System.out.println("Agency name: ");
                    agencyName = in.nextLine();
                    in.nextLine();

                    System.out.println("No. of days: ");
                    days = in.nextInt();



                    Patuvanje p = new PraznicnoPatuvanje (agencyName, 20, days);
                    nizaPatuvanje[i] = p;


  }

try this

Ketan G
  • 507
  • 1
  • 5
  • 21