-4

I am new to Java programming. I have been trying to figure out this issue for days now. The first loop skips the if statements and gives me "invalid entry", subsequent runs are fine. I have watched numerous videos and have read multiple questions/answers but still can not figure it out.

Any help or hint is greatly appreciated. Thanks

Sorry about not posting the picture instead of the code.

My code

package com.Class5;

import java.util.Scanner;

public class practice {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);


    System.out.println("Number of loops");
    int numberOfRuns=input.nextInt();


    for(int i=0;i<=numberOfRuns;i++) {

        String month;
        System.out.println("What month were you born in?");
        month=input.nextLine();

        if(month.equalsIgnoreCase("December")||month.equalsIgnoreCase("January")||month.equalsIgnoreCase("February")) {
            System.out.println("You were born in Winter");
        }else if(month.equalsIgnoreCase("March")||month.equalsIgnoreCase("April")||month.equalsIgnoreCase("May")){
            System.out.println("You were born in Spring");
        }else if(month.equalsIgnoreCase("June")||month.equalsIgnoreCase("July")||month.equalsIgnoreCase("August")) {
            System.out.println("You were born in Summer");
        }else if(month.equalsIgnoreCase("September")||month.equalsIgnoreCase("October")||month.equalsIgnoreCase("November")) {
            System.out.println("You were born in fall");
        }else {
            System.out.println("invalid entry");
        }


    }



}

}

1 Answers1

-1

The reason why you got problems with the skipping loop is that the nextLine is skipped. its a line Termination issue, you should check if the nextLine contains any data, if not then you can reset the for to the last iteration.

BlackNetworkBit
  • 776
  • 10
  • 21