Results 1 to 5 of 5

Thread: New to Arrays

  1. #1
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default New to Arrays

    My project is to write a program using arrays that will does the following:
    1. Asks what month it is
    2. Asks what the average rainfall is for that month.
    3. Asks what the current rainfall is for that month.
    4. Repeat with previous month, and repeat until the user says to quit.
    5. When the user quits, print out the info in a formatted table:
    JAN FEB MAR ETC
    AVG RAIN 1.00 1.5 3.3
    LAST RAIN 2.00 3.5 1.1

    Any help appreciated. This is my first time working with Arrays so please keep it simple!

    Tip: The assignment says "There are a variety of ways to deal with the month names. One straightforward method is to code the months as integers and then do a conversion to a string for the month name before doing the output".

    Basically, I am stuck at square one. So thanks for any tips or help.

  2. #2
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    As you can see, I have not gotten very far!

    Java Code:
    import java.util.Scanner;
    
    public class Rainfall
     {
      public static void main(String[] args)
    
      	{
        int month;
    	int index;
        String a[]={"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
        System.out.println("Name of months in a year : ");
    
    for(int i=1;i<a.length;i++){
          System.out.println(a[i]);
    
          Scanner keyboard = new Scanner(System.in);
          System.out.println("What month do you want to start with?  Enter numeric value for the month:");
          index = keyboard.nextInt();
    
        }
      }
    }

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you have 12 months, and each month has an array of 2 integers to store (about rainfall), so that gives you an array of 12 with each item having a sub-array of 3 items (two dimensional):

    Java Code:
    //declare two-dimensional array
    Object[][] months = new Object[12][];
    //set data
    months[0] = new Object[] {"January",10,10};
    months[1] = new Object[] {"February",5,6};
    ...
    months[11] = new Object[] {"December",20,20};

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    It's a bit hard to see a specific question here.

    Does that code compile? If it does not and you can't understand the compiler's messages, post them.

    If the code compiles but does not do what you expect (or intend), say what it does and what you were expecting.

  5. #5
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    Ahhh, I scanned ahead in the book and they do go over two dimensional arrays. Typical of this teacher to assign projects based on things she hasn't gone over yet. I'll read that part and get back to you guys. You are always such great help, thanks.

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  3. Please help me with arrays!
    By ddrcan in forum New To Java
    Replies: 2
    Last Post: 08-10-2009, 05:30 PM
  4. Help!! With arrays
    By ookie833 in forum New To Java
    Replies: 8
    Last Post: 12-14-2008, 01:57 AM
  5. Arrays
    By TheRocket in forum New To Java
    Replies: 6
    Last Post: 12-10-2008, 07:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •