-1

I am writing java code to assign inputs from the user to three variables (of type double, string, int). I used scanner for this purpose. It compiled and run perfectly without any error.

My Problem:
I could only input one to the variable of type double (first variable). I couldn't input value to two other variables. Can you explain why this is happening and fix this issue for me?

public class Main {

    public static void main(String[] args) {
        double revenueTill;
        String month;
        int dayOfMonth;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter revenue until now: ");
        revenueTill = scanner.nextDouble();

        System.out.println("Enter month: ");
        month = scanner.nextLine();

        System.out.println("Enter day of month: ");
        dayOfMonth = scanner.nextInt();

        MonthPrediction monthPrediction = new MonthPrediction(revenueTill, month, dayOfMonth);
        monthPrediction.showRevenueOfMonth(revenueTill, month, dayOfMonth);
    }
}
Yunnosch
  • 21,438
  • 7
  • 35
  • 44
  • 1
    [Scanner skipping nextLine after nextFoo](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo). This is most probably the issue, check out the answers in the linked question for a fix. – maloomeister Sep 08 '20 at 06:07
  • Try using next() instead of nextLine() for your second input. – ArbitraryChoices Sep 08 '20 at 06:08

1 Answers1

-1

Try changing nextLine() to next():

System.out.println("Enter month: ");
month = scanner.next();

System.out.println("Enter day of month: ");
dayOfMonth = scanner.nextInt();
Seema
  • 84
  • 6