Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Horoscope program running but not as intended

  1. #1
    Junior Member
    Join Date
    Aug 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Horoscope program running but not as intended

    import java.util.Scanner;
    public class Horoscope {
    	public static void main(String[] args) {
    		System.out.println("Enter you birth month:");
    		Scanner sc = new Scanner(System.in);
    		String mon = sc.nextLine();
    		System.out.println("Enter you birth day");
    		int day = sc.nextInt();
    		String sign = "";
    		if((mon == "January" && day >= 21) || (mon == "February" && day <= 18)) {
    			sign = "Aquarius";
    		}
    		else if((mon == "February" && day >= 19) || (mon == "March" && day <= 20)) {
    			sign = "Pisces";
    		}
    		else if((mon == "March" && day >= 21) || (mon == "April" && day <= 20)) {
    			sign = "Aries";
    		}
    		else if((mon == "April" && day >= 21) || (mon == "May" && day <= 20)) {
    			sign = "Taurus";
    		}
    		else if((mon == "May" && day >= 21) || (mon == "June" && day <= 21)) {
    			sign = "Gemini";
    		}
    		else if((mon == "June" && day >= 22) || (mon == "July" && day <= 22)) {
    			sign = "Cancer";
    		}
    		else if((mon == "July" && day >= 23) || (mon == "August" && day <= 23)) {
    			sign = "Leo";
    		}
    		else if((mon == "August" && day >= 24) || (mon == "September" && day <= 23)) {
    			sign = "Virgo";
    		}
    		else if((mon == "September" && day >= 24) || (mon == "October" && day <= 23)) {
    			sign = "Libra";
    		}
    		else if((mon == "October" && day >= 24) || (mon == "November" && day <= 22)) {
    			sign = "Scorpio";		
    		}
    		else if((mon == "Nomember" && day >= 23) || (mon == "December" && day <= 21)) {
    			sign = "Sagittarius";
    		}
    		else{
    			sign = "Capricorn";
    		}
    		System.out.println(sign);
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Horoscope program running but not as intended

    Can you post the output for what the program does now
    and explain what is wrong
    and show an example of what the desired output is?


    Note: Use the equals method to compare the contents of String objects. Do not use the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Horoscope program running but not as intended

    You can't have a singular scanner variable being used for picking up both String and Integer input.
    Change this:
    Scanner sc = new Scanner(System.in);
    String mon = sc.nextLine();
    System.out.println("Enter you birth day");
    int day = sc.nextInt();
    To this
    Scanner ss = new Scanner(System.in);
    Scanner sc = new Scanner(System.in);
    String mon = ss.nextLine();
    System.out.println("Enter you birth day");
    int day = sc.nextInt();
    (and adjust the rest of your code accordingly)

Similar Threads

  1. Running my Java first program
    By gauthamtin15 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 5th, 2014, 07:02 AM
  2. HELP About Javascript, creating program zodiacsign (horoscope)
    By jedmustdie in forum Other Programming Languages
    Replies: 16
    Last Post: March 13th, 2014, 08:55 AM
  3. running java program without jdk installed
    By eku in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 25th, 2014, 08:28 AM
  4. Replies: 21
    Last Post: June 12th, 2013, 11:33 AM
  5. Java program not running in command prompt
    By Padmahasa in forum Computer Support
    Replies: 1
    Last Post: August 10th, 2012, 07:22 AM