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

Thread: need help with array

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default [Solved] need help with array

    So I am kinda new to java and was given a program to do.

    a. Create one or more arrays to hold the names of the months and the number of days in each month. Assume that February always has 28 days.

    b. Display the contents of the array(s).

    c. Ask the user for a number to represent the month: 1 = Jan, 2 = Feb, etc. Search for the corresponding month and display the name of the month and the number of days. If the number is not found, display an error message.

    d. Ask the user for the name of a month. Search for the corresponding month and display the number of the month and the number of days. If the month is not found, display an error message.

    I have done a-c but i am having trouble with d.


    package array;
    import java.util.Scanner;

    public class Main
    {

    public static void main(String[] args)
    {
    int [] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Array holds number of days in month
    String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; // array holds months


    for (int index = 0; index < months.length; index++) // loop to display number of days in month
    {
    System.out.println(months[index] + " has " + // displays number of days in month
    days[index] + " days ");
    }

    Scanner keyboard = new Scanner(System.in); // creates scanner
    System.out.println("Enter a number to represent the month");
    int search = keyboard.nextInt(); // saves user value into search

    search--; // subtracts 1 from user input to account for the index of 0

    if(search > months.length)

    System.out.println("Can not find number"); // yes if search is more then months display error
    else

    System.out.println( months[search] + " has " + days[search] + " days "); // no if search is less then months display contents


    Scanner string = new Scanner(System.in); //creates new scanner
    System.out.println("Enter the name of the month");
    String input = string.nextLine(); // saves user strin in to input

    for (int index = 0; index < months.length; index++) //loop to compare user input to months array
    {
    if(input.equals(months[index])) // yes display month and days
    System.out.println(months[index] + " has " + // displays number of days in month
    days[index] + " days ");
    else
    if(!(input.equals(months[index]))) // no display error
    System.out.println("can not find month");
    }
    }

    }

    Its kinda hard to explain the problem but if you run it you will see when it asks the user to input the name of the month that is where the error occurs. Any help would be appreciated.
    Last edited by wolfrain62; June 11th, 2011 at 12:18 AM.


  2. #2
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: need help with array

    Quote Originally Posted by wolfrain62 View Post
    So I am kinda new to java and was given a program to do.

    a. Create one or more arrays to hold the names of the months and the number of days in each month. Assume that February always has 28 days.

    b. Display the contents of the array(s).

    c. Ask the user for a number to represent the month: 1 = Jan, 2 = Feb, etc. Search for the corresponding month and display the name of the month and the number of days. If the number is not found, display an error message.

    d. Ask the user for the name of a month. Search for the corresponding month and display the number of the month and the number of days. If the month is not found, display an error message.

    I have done a-c but i am having trouble with d.

    [CODE]
    package array;
    import java.util.Scanner;
     
    public class Main
    {
     
        public static void main(String[] args)
        {
            int [] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Array holds number of days in month
            String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; // array holds months
     
     
            for (int index = 0; index < months.length; index++) // loop to display number of days in month
            {
             System.out.println(months[index] + " has " + // displays number of days in month
                                days[index] + " days ");
            }
     
            Scanner keyboard = new Scanner(System.in); // creates scanner
            System.out.println("Enter a number to represent the month");
            int search = keyboard.nextInt(); // saves user value into search
     
            search--; // subtracts 1 from user input to account for the index of 0
     
            if(search > months.length)
     
                System.out.println("Can not find number"); // yes if search is more then months display error
            else
     
                System.out.println( months[search] + " has " + days[search] + " days "); // no if search is less then months display contents
     
     
            Scanner string = new Scanner(System.in); //creates new scanner
            System.out.println("Enter the name of the month");
            String input = string.nextLine(); // saves user strin in to input
     
           for (int index = 0; index < months.length; index++) //loop to compare user input to months array
           {
            if(input.equals(months[index])) // yes display month and days
               System.out.println(months[index] + " has " + // displays number of days in month
                                  days[index] + " days ");
            else
                if(!(input.equals(months[index]))) // no display error
                System.out.println("can not find month");
           }
        }
     
    }
    [/CODE]
    For future reference, use the CODE tags and the java highlighting tags. Check out the new people thread if you want to know how to do that.

    I will take another look at your code now that I have highlighted it (and CODE tagged it) and get back to you.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with array

    Ok thanks I noe what the problem is just do not noe how to fix it.

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: need help with array

    Your problem is that the FOR loop is going through and printing lines for every one of the months. It then prints the correct line only for the matching month (with a bunch of could not find month statements).

    I haven't thought of a solution yet (I'm kind of tired). I will try. Maybe in the mean time one of the Java experts here will come along and answer it!

    I will let you know if I find something.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: need help with array

    import java.util.Scanner;
     
    public class Main
    {
     
        public static void main(String[] args)
        {
            boolean success = false;
            int [] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Array holds number of days in month
            String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; // array holds months
     
     
            for (int index = 0; index < months.length; index++) {  // loop to display number of days in month
             System.out.println(months[index] + " has " + // displays number of days in month
                                days[index] + " days ");
            }
     
            Scanner keyboard = new Scanner(System.in); // creates scanner
            System.out.println("Enter a number to represent the month");
            int search = keyboard.nextInt(); // saves user value into search
     
            search--; // subtracts 1 from user input to account for the index of 0
     
            if(search > months.length) {
              System.out.println("Can not find number"); // yes if search is more then months display error
            } else {
              System.out.println( months[search] + " has " + days[search] + " days "); // no if search is less then months display contents
     
            Scanner string = new Scanner(System.in); //creates new scanner
            System.out.println("Enter the name of the month");
            String input = string.nextLine(); // saves user strin in to input
     
           for (int index = 0; index < months.length; index++) { //loop to compare user input to months array
             if(input.equals(months[index])) {   // yes display month and days
               System.out.println(months[index] + " has " + days[index] + " days ");
               success = true;
             } else {
               success=false; //This is unnecessary.  I just hate blank else statements.
             }
           }
           if(success == false) {
             System.out.println("Cannot find month");
           }
        }
      }
    }

    This is the solution I came up with. Not THE MOST efficient way of doing it (I'm just tired and this was simple ). Oh and the second success=false is unnecessary. I just hate blank else statements

    Idk if that is legal in your assignment or not but it works.

    All I did was declare a boolean success = false after the opening of the main method.

    Then I put true after the if.equals.

    I moved the error to outside the FOR loop so it won't keep printing it.

    Hope that helps!

  6. The Following User Says Thank You to bgroenks96 For This Useful Post:

    wolfrain62 (June 11th, 2011)

  7. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with array

    Ahh thank you very much it working perfectly. I knew i should have used a Boolean

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  3. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  4. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 12:54 PM
  5. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM