Skip to content
Advertisement

problems at splitting strings in java

I’m new to java and I want to know how to splits strings with values.

I had an array like this :

JavaScript

and I want to divide each string into 3 values, the name (String) the x-value (double), and the y-value (double)

firstly I thought of cutting at every space, but for the cities like San Francisco, it won’t work 🙁 any ideas?

Advertisement

Answer

I suspect this assignment was meant for the OP to learn about the lastIndexOf and substring methods of the String class.

Here are the test results from one of my many tests.

JavaScript

Here’s the complete runnable code. The parseCities method finds the position of the last two spaces and uses substring to “split” the String into three pieces.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement