# Multidimensional Array help

• 04-09-2011, 02:17 AM
Gaebril
Multidimensional Array help
I'm learning how to implementing Multidimensional arrays and am having a problem properly using them. I will paste the prompt, then the code I have for the assignment thus far.

Assignment:
Modify your prior Lab by adding an array of objects. Within the loop, instantiate each individual object. Make sure the user cannot keep adding another Foreign conversion beyond your array size.
After the user selects quit from the menu, prompt if the user want to display a summary report. If they select ‘Y’ then, using your array of objects, display the following report:

Item Conversion Dollars Amount
1 Japanese Yen 100.00 32,000.00
2 Mexican Peso 400.00 56,000.00
etc.

Number of Conversions = 3

Here is the code I have thus far. I understand that I probably need a 2dimensional array to accomplish the above result, but I can't seem to get a 2dimensional array to work while using the method to set a Maximum.

Code:

```import java.util.Scanner; public class lab8 {         public static void main(String[] args)         {                 int choice;                 String s;                 char report;                 int x = 0;                 final int Max=3;                 Foreign[] trade = new Foreign[Max];                 Scanner input=new Scanner(System.in);                                 Foreign.title();                 do                 {                         trade[x] = new Foreign();                         trade[x].menu();                         choice=trade[x].selection;                                                 if (choice>= 1 && choice<= 4)                         {                         trade[x].dollars();                         trade[x].amount();                         trade[x].vertical();                         System.out.println(trade[x]);                         System.out.println("\n");                         x++;                         }                                         else if (choice > 4)                                         {                                         System.out.println("Please select 1 through 4, or 0 to quit");                                         }                 }                 while (choice != 0 && x < Max);                 System.out.println("Would you like to see a summary report? Y/N");                 s=input.nextLine();                 s=s.toUpperCase();                 report=s.charAt(0);                                         if(report == 'Y')                 {                         System.out.println("\nItem\t\tConversion\t\tDollars\t\tAmount");                         for (int i=0; i < x; i++)                         {                         System.out.println(i+1 + "\t");                                                                                 System.out.print(trade[i] + "\t");                                                 }                 }                                 Foreign.counters();         } }```
then the classes/objects
Code:

```import java.util.Scanner; import java.text.NumberFormat; import java.text.DecimalFormat; public class Foreign { static Scanner read=new Scanner(System.in); public static double rate; public static String country; public static int selection; private static int count=0, total=0; private static String[] types= {"Canadian Dollars","Mexican Peso","Japanese Yen","Euro"}; private static double[] rates= {1.1553,10.550,117.57,.8407}; private double dollars, amount; static NumberFormat dollarForm=NumberFormat.getCurrencyInstance(); static DecimalFormat currency=new DecimalFormat("###,###.00"); public Foreign() { selection=0; dollars=0; amount=0; } public static void title() { System.out.println("Foreign Exchange calculator"); }         public static void menu()         {         System.out.println("U.S. Dollar to Foreign Currency Exchange");         System.out.println("1. U.S. to Canada");         System.out.println("2. U.S. to Mexico");         System.out.println("3. U.S. to Japan");         System.out.println("4. U.S. to Euro");         System.out.println("0. Quit\n");         System.out.print("\nPlease enter your choice:");         selection=read.nextInt();         }        public void dollars() {                 System.out.print("\nPlease enter the amount of U.S. Dollars: ");                 dollars= read.nextDouble();                 count++;                 total+=dollars;                }         public void amount()         {         rate=rates[selection-1];         amount=dollars*rate;         } public void vertical() { country=types[selection-1]; System.out.println("Country = " +country); System.out.println("Rate = " +dollarForm.format(rate)); System.out.println("Dollars = " +dollarForm.format(dollars)); System.out.println("Converted value = " +dollarForm.format(amount)); } public static void counters() { System.out.println("\n The total transaction(s): " +count); System.out.println(" The total amount converted is: " +dollarForm.format(total)); } public String toString() {                 String horizontal;                 horizontal = country + " " + rate + " " +dollarForm.format(dollars) + " " +currency.format(amount);                 return horizontal; } }```
I'm sure the problem lies in the first set of code. As of now, when calling on the Array it will just pull my toString. I need to figure out how to get the desired chart. I think I need to use an ArrayList but I'm not sure how to implement that into the code.
• 04-09-2011, 08:36 PM
Coldsource
To make a 2 dimensional array, think of it as a chart, something you would see in excel. You have rows and columns, having an output in a chart format would look something like this.

Code:

``` int[][] array = new int[][] {{1, 2, 3} , {2, 1, 1}, {3, 2, 2}, {4, 3, 3}}; for (int i = 0 ; i < 4 ; i++) {       System.out.println("");       for (int j = 0 ; j < 3 ; j++)       {             System.out.println(array[i][j] + " ");       } }```

1 2 3
2 1 1
3 2 2
4 3 3

The nested for loop is generating a column in a row, and the outer for loop is generating a new row, one line down.

Every new section { int , int , int } is basically another row. Another way to input information into an array would look like this.

Code:

``` int[][] array = new int[][]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 2; array[1][1] = 1; array[1][2] = 1; array[2][0] = 3; array[2][1] = 2; array[2][2] = 2;```
Whats going on here is an array[row][column].

So in your code you should have a maxXValue, maxYValue.