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 9 of 9

Thread: Chinese Zodiac array

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Chinese Zodiac array

    Hey, so I think I did the array right for this, but I'm trying to figure out how to get a certain String out of the array when the user inputs the year they were born. For example, if I input 1993, then I would want the String[Rooster] to be printed. Please Help
    import java.util.Scanner;
    import java.util.Arrays;
     
    public class Chinese_Zodiac {
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
    		int year = 0;
    		int zodiacYear = 0;
     
    		//Zodiacs(in order): Monkey, Rooster, Dog, Pig, Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep
    		//Create array
    		String[] zodiacs = {"Monkey", "Rooster", "Dog", "Pig", "Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Sheep"};
     
    		//User inputs year born and gets zodiac
    		System.out.println("Please enter the year you were born: ");
    		year = input.nextInt();
     
    		zodiacYear = year % 12;
    		System.out.println(zodiacYear + " " + zodiacs);	
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: Chinese Zodiac array

    You need a 'decoder' that equates the year entered to the ranges of years that correspond to the appropriate index of the array. Search for Chinese Zodiac, use the resulting description of which years equate to which sign, and develop an algorithm to pull the right sign (index) from the array given a specific year.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Chinese Zodiac array

    Wouldn't that be the same as doing
    System.out.println("Please enter the year you were born: ");
    		year = input.nextInt();
     
    		zodiacYear = year % 12;
    Since when they input the year, when you get the remainder from 12, it should equal the segment of the array that the correct zodiac is in?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Chinese Zodiac array

    If the String you want is in an array, you need to get an element from that array using the value you computed as the index to that array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Elyril (March 9th, 2014)

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: Chinese Zodiac array

    Did you look up the Chinese Zodiac? It's not month-based.

  7. #6
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Chinese Zodiac array

    Yes, and every 12 years the animal repeats, so inputting a year, like 1993, and dividing by 12 shouldn't be a month. It was what was in the book, and I tested it before and it worked, but I just don't know how to apply it to arrays.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Chinese Zodiac array

    don't know how to apply it to arrays.
    Did you see post#4?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: Chinese Zodiac array

    Quote Originally Posted by Elyril View Post
    Yes, and every 12 years the animal repeats, so inputting a year, like 1993, and dividing by 12 shouldn't be a month. It was what was in the book, and I tested it before and it worked, but I just don't know how to apply it to arrays.
    Ahh. Describing what you're thinking is helpful. That way we don't have to guess.

    As Norm has been trying to get you to see, what is the number you get from the year % 12 equation? More specifically, what could you do with that number? You might also need to include 1924 in the equation, something like:

    ( usersYear - 1924 ) % 12 = ?

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    Elyril (March 9th, 2014)

  11. #9
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Chinese Zodiac array

    Okay.. yeah, I actually put
    zodiacYear = (year) % 12;
    		System.out.println("You were born in " + year + " and your Chinese Zodiac is "  + zodiacs[zodiacYear]);
    and I found the years for the Zodiacs, but it confused me for a bit cause they started either Jan. or Feb. then ended the Jan or Feb of that next year so I had to think about it first, but now I got it. Thanks guys!

Similar Threads

  1. how to read chinese data from excel to java
    By swapna in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: November 17th, 2011, 06:02 AM
  2. Chinese Remainder therom
    By Joy123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 16th, 2011, 07:11 AM
  3. Replies: 1
    Last Post: April 7th, 2011, 07:01 AM
  4. Vietnamese Character Problem in JSP Report. Chinese and Thai Char OK
    By mrvora in forum JavaServer Pages: JSP & JSTL
    Replies: 7
    Last Post: October 23rd, 2009, 02:35 AM
  5. Certain Chinese Characters not displayed properly.
    By kerwintang in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 20th, 2009, 08:23 AM

Tags for this Thread