# Thread: split() and multi dimensional array

1. Member
Join Date
Jan 2015
Posts
21
Rep Power
0

## split() and multi dimensional array

I've found different examples on line, but none that use the split method with a multidimensional array. I would like a user to input coordinates (x,y) for city locations and store them into a 2-D array. For the user to input the x,y-coordinates on one line I need to use split(',') and then parse the string array to a double which will then be used to calculate the distances from one another.

My issue is how to store the String vales into the 2-D array. I do not want to store the x-value at even (cityArray[0]) and y-value at odd (cityArray[1]) 1-D locations.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: split() and multi dimensional array

So are you asking how to use a 2D array? Once you get the x,y coordinates, then you assign them:

cityArray[x][y] = some value.

Regards,
Jim

3. Member
Join Date
Jan 2015
Posts
21
Rep Power
0

## Re: split() and multi dimensional array

I want to user to be able to enter the x,y coordinates on a single line, so that I don't need to prompt the user to enter the x-component and then prompt them again to enter the y-component. I would prefer the user be able to do it from a single prompt.
For example:
Enter the coordinate (x,y) of the city: 10,15

Then split and store each component as part of a 2-D array. I was only able to find an example of the x,y being stored in a 1-D array here : http://stackoverflow.com/questions/2...e-line-in-java

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: split() and multi dimensional array

x and y are the locations at which to store something. So use split to return an array of type String. Then convert the values of the array to ints using Integer.valueOf(String).

Regards,
Jim

5. Member
Join Date
Jan 2015
Posts
21
Rep Power
0

## Re: split() and multi dimensional array

Yes, I know that. What I am asking is how do I split the String the user inputs into just an X that I can put into one column of the array and then the y into the other column.
Converting the string is not my problem.

I just want to separate the input and store each component in the 2-D array. How do I do that?!

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: split() and multi dimensional array

Originally Posted by the_dude
I just want to separate the input and store each component in the 2-D array. How do I do that?!
You can only store a single component at the x and y location. However, if you have multiple things to store there you should create a class to do so.

Java Code:
```Thing oneThing = new Thing(int thing1, String thing2); // apologies to Dr. Seuss
Thing [][] things = new Thing[10][10]; // a 10 x 10 array of Things
things[5][6] = oneThing;

class Thing {
// thing stuff goes here
}```

Of course, if you want to store x and y at location (x,y) then why? Once you know the index you know the values.

Regards,
Jim

7. ## Re: split() and multi dimensional array

Jim, I think the requirement is to store x and y at [i, 0] and [i, 1]

db

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: split() and multi dimensional array

Originally Posted by DarrylBurke
Jim, I think the requirement is to store x and y at [i, 0] and [i, 1]

db
Well, I still don't know what the issue is. Perhaps I should let someone else try and help the OP.

Regards,
Jim

9. ## Re: split() and multi dimensional array

how to store the String vales into the 2-D array
It's normal to talk about items in a two dim array in terms of rows and columns. Each row has 0 or more columns.
In Darryl's example there were at least 2 columns on the row with indexes 0 and 1. The row was indexed by i.

10. Member
Join Date
Jan 2015
Posts
21
Rep Power
0

## Re: split() and multi dimensional array

Darryl, you are right. That's all I want to do is separate a single line of input with two different variables -- the x and y. Where I want to store it in the format you've suggested. Is there a way to split the data if the user were to enter it as x,y on the console line? I saw where you can use the split(',') to separate the values, but I don't know how to store it in the 2-D array : [i,0] for x and [i,1] for y.

11. ## Re: split() and multi dimensional array

store it in the 2-D array : [i,0] for x and [i,1] for y.
The syntax for accessing an element of a 2 dim array: theArray[rowIndex][columnIndex]

12. Member
Join Date
Jan 2015
Posts
21
Rep Power
0

## Re: split() and multi dimensional array

I don't understand what is so unclear about what I'm saying. Clearly I'm not explaining my problem effectively.
I understand arrays, I know how to access data from them.

Problem: USING THE SPLIT METHOD

The user will input the x,y coordinates/location of a city. All I want to do is allow the user to enter each component on one line. that's it.
I don't want to do the following:
System.out.println("Enter the x coordinate: " );
int x = input.nextInt();
System.out.println("Enter the y coordinate: ");
int y = input.nextInt();

Instead, I want to do this:
System.out.println("Enter the location (x,y) of the city: );
String data = input.nextLine();
......after this, I need to know how to split the string, and then assign the x value to [i,0] and the y value to [i,1]. That's it.

13. Member
Join Date
Jan 2015
Posts
21
Rep Power
0

## Re: split() and multi dimensional array

So once I store the coordinates for, let's say 5 cities, I will then find the distance relative to the other cities. I just want to learn how to make the code more sophisticated by doing what I stated in previous post.

14. ## Re: split() and multi dimensional array

What have you tried?
You said this in post#10
you can use the split(',') to separate the values,
Have you tried that?

I understand arrays, I know how to access data from them.
The syntax for accessing an element of a 2 dim array:
Java Code:
```   aVar = theArray[rowIndex][columnIndex];  // get data from an array
theArray[rowIndex][columnIndex] = aVar2;  // store data in an array```
Last edited by Norm; 02-03-2015 at 12:28 AM.

15. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: split() and multi dimensional array

Originally Posted by the_dude
Darryl, you are right. That's all I want to do is separate a single line of input with two different variables -- the x and y. Where I want to store it in the format you've suggested. Is there a way to split the data if the user were to enter it as x,y on the console line? I saw where you can use the split(',') to separate the values, but I don't know how to store it in the 2-D array : [i,0] for x and [i,1] for y.
Do you mean this?

Java Code:
```int myArray[][] = new int[10][2];
myArray[i][0] = x;
myArray[i][1] = y;```
Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•