Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: split() and multi dimensional array

  1. #1
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default 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. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default 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. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: split() and multi dimensional array

    Quote Originally Posted by the_dude View Post
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    27

    Default Re: split() and multi dimensional array

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

    db
    the_dude likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: split() and multi dimensional array

    Quote Originally Posted by DarrylBurke View Post
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default 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. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default 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]
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default 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. #13
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default 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. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: split() and multi dimensional array

    Quote Originally Posted by the_dude View Post
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. multi or two dimensional array
    By maya700 in forum New To Java
    Replies: 4
    Last Post: 07-12-2010, 07:52 PM
  2. [SOLVED] Multi-dimensional Array further help
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 05-07-2009, 05:50 PM
  3. Multi-dimensional array
    By VinTiger in forum New To Java
    Replies: 22
    Last Post: 03-01-2009, 07:51 AM
  4. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 04:34 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:00 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
  •