Hi there,

I am trying to get the words (city names) from a user input. For example, the inputs:

String input1 = "Pyeongchang 135135.13 531.351";  //Note: "Pyeongchang" is of one word.
String input2 = "New York 3543.25 25352.523532";  //Note: "New York" is of two words.

I need to be most efficient, time wise.

At this stage, I am only able to get a one-word city by doing this:

Scanner s = new Scanner(System.in);  //These input would be like input1 and input2, shown above.
String cityName = s.next();    //This only works for one-worded cities like Pyeongchang, but not for >1 worded cities.

How would you change the code so that it reads the names of any lengths, and be most efficient?

Cheers.

Just pointing this out, the Scanner class only reads the input so whatever you write that's what its going to read despite how long or how many words it has. Thus I think your problem is when you are parsing the input that you are getting errors, which the Scanner takes no role in whatsoever as far as I know.

If what you want is to separate is the letters(city name) from the numbers, what I would do is iterate(left to right) through the string until you reach a digit than use the substring method from the String class to get the string from 0 to the index right before the first digit is encounter.

...iterate(left to right) through the string until you reach a digit...

How do you do this?

I've thought about this too, but have no idea what to search for in Google to get this answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.