This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Multi-Dimensional Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multi-Dimensional Arrays" Watch "Multi-Dimensional Arrays" New topic
Author

Multi-Dimensional Arrays

Alan Arkwell
Greenhorn

Joined: Apr 21, 2005
Posts: 10
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.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Along with the suggestion of creating objects and then storing them in the array, you could also look at using HashMap.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multi-Dimensional Arrays