Issue with Scanner - String not working

Trying to get a horoscope program to determine sign for entered date. Date is int while Month is String. When started, the program will ask for the date, but after that it skips the Month input and println “capricorn” which is at the last else statement of the program. Can’t seem to figure out why it won’t let you input the Month.

import java.lang.Math;
import java.util.Scanner;

public class Main{
public static void main(String args){
Scanner scan = new Scanner (System.in);
System.out.println("input your date : ");
int date = scan.nextInt();
System.out.println("input your month : ");
String month = scan.nextLine();

    if((date>21&&month=="December")||(date<=19&&month=="January")){
        System.out.println("Your zodiac sign is Capricorn");
    }
    else if((date>20&&month=="January")||(date<=18&&month=="February")){
        System.out.println("Your zodiac sign is Aquarius");
    }
    else if((date>19&&month=="February")||(date<=20&&month=="March")){
        System.out.println("Your zodiac sign is Pisces");
    }
    else if((date>21&&month=="March")||(date<=20&&month=="April")){
        System.out.println("Your zodiac sign is Aries");
    }
    else if((date>21&&month=="April")||(date<=20&&month=="May")){
        System.out.println("Your zodiac sign is Taurus");
    }
    else if((date>21&&month=="May")||(date<=20&&month=="June")){
        System.out.println("Your zodiac sign is Gemini");
    }
    else if((date>21&&month=="June")||(date<=20&&month=="July")){
        System.out.println("Your zodiac sign is Cancer");
    }
    else if((date>21&&month=="July")||(date<=20&&month=="August")){
        System.out.println("Your zodiac sign is Leo");
    }
    else if((date>21&&month=="August")||(date<=22&&month=="September")){
        System.out.println("Your zodiac sign is Virgo");
    }
    else if((date>23&&month=="August")||(date<=20&&month=="October")){
        System.out.println("Your zodiac sign is Libra");
    }
    else if((date>21&&month=="Octtober")||(date<=22&&month=="November")){
        System.out.println("Your zodiac sign is Scorpio");
    }
    else{
        System.out.println("Your zodiac sign is Sagitarius");
    }
}

}

I answered a very similar question not that long ago: Reading input from user - #3 by jmrunkle

Check that topic and come back here if you still have questions.

It allows you to input the month now, but it bypasses the if statements and goes straight to the else statement at the bottom ( Sagittarius). It doesn’t look at the month string at all.

Yes, because of the reason I mentioned in the other thread.

When the scanner reads the input for the next int, it buffers the newline character that you used to finish inputting the number. So when you call nextLine it returns an empty string. You can clear the buffer by calling nextLine a second time, otherwise you have to use a different way of reading the input.

For example you could always use nextLine and parse the String into an int for the first part.

The minimal change that should fix your program is this:

Scanner scan = new Scanner (System.in);
System.out.println("input your date : ");
int date = scan.nextInt();
scan.nextLine(); // clear the newline
System.out.println("input your month : ");
String month = scan.nextLine();
1 Like