# Issue with Scanner - String not working

**URL:** <https://forum.codewithmosh.com/t/issue-with-scanner-string-not-working/16360>\
**Category:** Java\
**Created:** [November 8, 2022, 5:37pm UTC](https://forum.codewithmosh.com/t/issue-with-scanner-string-not-working/16360 "2022-11-08T17:37:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tytan](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@Tytan](https://forum.codewithmosh.com/u/Tytan)\
**Post date:** [November 8, 2022, 5:37pm UTC](https://forum.codewithmosh.com/t/issue-with-scanner-string-not-working/16360/1 "2022-11-08T17:37:57Z")

</div>

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");
    }
}

```

}

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [November 9, 2022, 9:25pm UTC](https://forum.codewithmosh.com/t/issue-with-scanner-string-not-working/16360/2 "2022-11-09T21:25:53Z")

</div>

I answered a very similar question not that long ago: [Reading input from user - #3 by jmrunkle](https://forum.codewithmosh.com/t/reading-input-from-user/15035/3)

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

---

<div class="post-metadata">

**Author:** ![Tytan](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@Tytan](https://forum.codewithmosh.com/u/Tytan)\
**Post date:** [November 9, 2022, 10:33pm UTC](https://forum.codewithmosh.com/t/issue-with-scanner-string-not-working/16360/3 "2022-11-09T22:33:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [November 10, 2022, 4:09am UTC](https://forum.codewithmosh.com/t/issue-with-scanner-string-not-working/16360/4 "2022-11-10T04:09:44Z")

</div>

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:

```java
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();
```
