# Thread: New to Arrays

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

## 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. Member
Join Date
Mar 2011
Location
San Diego, CA
Posts
34
Rep Power
0
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. 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
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. Member
Join Date
Mar 2011
Location
San Diego, CA
Posts
34
Rep Power
0
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.

#### Posting Permissions

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