• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multi-Dimensional Arrays

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to multi-dimensional arrays and I am trying to create one that accepts a user input (name and home town).I am thinking that a MD array would store the two inputs alongside each other.Here is my code:
import javax.swing.JOptionPane;
public class AlansProg
{
public static void main(String[]args)
{

String Info[][] = new String[4][2];
for(int row=0;row<4;row++)
{

Info[row] = JOptionPane.showInputDialog("enter name");

}

for(int col=0;col<2;col++)

{

Info[col] = JOptionPane.showInputDialog("enter Home Town");

}

}
}
I am getting this error message :
incompatible types
found:java.lang.string
required:java.lang.String[]
Info[row] = JOptionPane.showInputDialog("enter name");
^
Am I right to think this can be done,or do I need to create seperate arrays?
Cheers...Alan.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're using a multidimensional array (an array of arrays), Info[row] is expecting another array. There's nothing wrong with multidimensional arrays, per se, but you should look at encapsulating related information in an object, then storing the object(s) in a Collection.

Object-Oriented Programming Concepts
Object-oriented language basics
Don't Fear the OOP

Java� Collections Framework
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you are using a multi-dimensional array, you need to provide multiple indices to access a single element. As you have it now, Info[row] gives you the whole row at that index. If you want to access a single String, you need to do something like Info[row][col] to get the individual element.

However, as noted above, this doesn't seem like a poper use for multidimentional arrays. Typically, an array (whether 1D, 2D, 3D, or more) should be used to hold similar data. In this case, each data item, while related, are not similar. It seems to me that a custom object is appropriate here to aggregate the data for an individaul. Then you can create a single-dimensional array (or a Collection) to hold many of these objects.

Layne
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We kinda beat you up on your example, but the answer about using two indexes was in there. While the custom object is a nicer solution, you may bump into arrays used this way. You might use some constants to document what's in teh columns:
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Along with the suggestion of creating objects and then storing them in the array, you could also look at using HashMap.
 
permaculture is largely about replacing oil with people. And one tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic