How to utilize data stored in a multidimensional array

1 answer

How do I display the multiple data's I stored in a multidimensional array in Java (eclipse)? I need to output the sweets recorded at each location and I am not sure how to go about that. The user needs to first input the sweet with the price at each location. After they have done all the inputs, there is supposed to be a menu outputted with the sweets they inputted at a specific location. What I do not understand is how to display the arrays which have stored the sweets inputted. My code can be displayed below

import java.util.Scanner; public class DessertExplosion {      public static void main(String[] args) {         Scanner input=new Scanner(System.in);         //Declare Variables         String dessert[][][]=new String[3][3][2];         double price[]=new double[18];         int counter=0;         String locationMenu="";         //Print menu statement         System.out.println("DESSERT EXPLOSION DAILY MENU ENTRY");         //Use for loop to determine dessert types and prices         for(int x=0;x<dessert.length;x++) {             for(int n=0;n<dessert[x].length;n++) {                 for(int i=0;i<dessert[x][n].length;i++) {                     counter++;                     //Oshawa location                     if (counter==1) {                         System.out.println("---Oshawa---");                         System.out.println("------------------------------");                         //Cakes                         System.out.println("Category: Cake");                         System.out.println("Enter Cake item #1");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cake $");                         price[i]=input.nextDouble();                         System.out.println("Enter Cake item #2");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cake $");                         price[i]=input.nextDouble();                         System.out.println("------------------------------");                         //Cookies                         System.out.println("Category: Cookie");                         System.out.println("Enter Cookie item #1");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");                         price[i]=input.nextDouble();                         System.out.println("Enter Cookie item #2");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");                         price[i]=input.nextDouble();                         System.out.println("------------------------------");                         //Ice Cream                         System.out.println("Category: Ice Crea,");                         System.out.println("Enter Ice Cream item #1");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");                         price[i]=input.nextDouble();                         System.out.println("Enter Ice Cream item #2");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");                         price[i]=input.nextDouble();                     }                     //Whitby Location                     else if(counter==2) {                         System.out.println("---Whitby---");                         System.out.println("------------------------------");                         //Cakes                         System.out.println("Category: Cake");                         System.out.println("Enter Cake item #1");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cake $");                         price[i]=input.nextDouble();                         System.out.println("Enter Cake item #2");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cake $");                         price[i]=input.nextDouble();                         System.out.println("------------------------------");                         //Cookies                         System.out.println("Category: Cookie");                         System.out.println("Enter Cookie item #1");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");                         price[i]=input.nextDouble();                         System.out.println("Enter Cookie item #2");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");                         price[i]=input.nextDouble();                         System.out.println("------------------------------");                         //Ice Cream                         System.out.println("Category: Ice Crea,");                         System.out.println("Enter Ice Cream item #1");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");                         price[i]=input.nextDouble();                         System.out.println("Enter Ice Cream item #2");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");                         price[i]=input.nextDouble();                     }                     //Ajax location                     else if(counter==3){                         System.out.println("---Ajax---");                         System.out.println("------------------------------");                         //Cakes                         System.out.println("Category: Cake");                         System.out.println("Enter Cake item #1");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cake $");                         price[i]=input.nextDouble();                         System.out.println("Enter Cake item #2");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cake $");                         price[i]=input.nextDouble();                         System.out.println("------------------------------");                         //Cookies                         System.out.println("Category: Cookie");                         System.out.println("Enter Cookie item #1");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");                         price[i]=input.nextDouble();                         System.out.println("Enter Cookie item #2");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " cookie $");                         price[i]=input.nextDouble();                         System.out.println("------------------------------");                         //Ice Cream                         System.out.println("Category: Ice Cream");                         System.out.println("Enter Ice Cream item #1");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");                         price[i]=input.nextDouble();                         System.out.println("Enter Ice Cream item #2");                         dessert[x][n][i]=input.next();                         System.out.println("Enter price for " + dessert[x][n][i] + " Ice Cream $");                         price[i]=input.nextDouble();                     }                 }             }         }      }  } 

All answers to this question, which has the identifier 61239363

The best answer:

You would want to do basically the same, which is using for loops going through your multidimensional array, retrieving each element with the indices of the loops then printing them out. You can use a space between each dessert printed to output the whole content. Since it's a 3 dimensional array, its display will depend on how you want to do it.

Personally, I'd display it as if it was a 2d array but the elements (in the 3rd array) are shown directly as arrays like this.

Arrays.toString(desserts[x][n]) 

But it depends on you.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data