Results 1 to 2 of 2
  1. #1
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default 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
    3 Canadian Dollar 100.00 156.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.

    Java 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
    Java 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.
    Last edited by Gaebril; 04-09-2011 at 02:32 AM.

  2. #2
    Coldsource is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default

    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.

    Java 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] + " ");
          }
    
    }
    Your output looks like this

    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.

    Java 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.

Similar Threads

  1. Multidimensional array - swap
    By BeginnerNoob in forum New To Java
    Replies: 13
    Last Post: 03-30-2011, 03:02 PM
  2. initialize multidimensional array
    By Aidoru in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 01:00 PM
  3. Retrieve Multidimensional Array ??
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 12-12-2009, 07:24 AM
  4. Navigate through a multidimensional array
    By VinTiger in forum New To Java
    Replies: 4
    Last Post: 05-05-2009, 07:16 PM
  5. [SOLVED] Multidimensional array
    By Torgero in forum New To Java
    Replies: 20
    Last Post: 03-22-2009, 11:36 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
  •