3

I have a textbox that gives out suggestions based on user input and one of my textboxes is location based.

The problem is, if a user types in Chicago,IL, everything works, but if they type in Chicago, IL, the suggestions stop. The only difference between the two is the space after the comma.

How can I fix this, so that even if a user puts in 2 or 4 spaces after the comma it still shows the same results as the first case?

This is my code:

if (location.contains(",")) {
// the city works correctly
   String city = location.substring(0, location.indexOf(","));

   // state is the problem if the user puts any space after the comma
   // it throws everything off  
     String state = location.substring(location.indexOf(",") + 1);


  String myquery = "select * from zips where city ilike ? and state ilike ?";

  }

I have also tried this:

 String state = location.substring(location.indexOf(",".trim()) + 1);

The string variables are used to make calls to the database; that is why I have to eliminate any spaces.

Makoto
  • 104,088
  • 27
  • 192
  • 230
user1591668
  • 2,591
  • 5
  • 41
  • 84

5 Answers5

3

How can I fix this, so that even if a user puts in 2 or 4 spaces after the comma it still shows the same results as the first case?

you can use location.replaceAll(" ", "")

for extracting the location into city,state you can use split() method as

String location[]=location.split(",");

Now

    String city=location[0];
    String state=location[1];

EDIT:(for Whome)

String location="New York, NY";
String loc[]=location.split(",");
String city=loc[0].trim();
String state=loc[1].trim();
System.out.println("City->"+city+"\nState->"+state);
Rustam
  • 6,485
  • 1
  • 25
  • 25
2

you were in the right direction by using trim(). However, you put it in the wrong place.
",".trim() will always yield ",". you want to trim the result of the substring operation:

String state = location.substring(location.indexOf(",") + 1).trim();
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
1

try using java.lang.String trim() function in the correct place.

trim on ",".trim() will produce ",".

Need to trim() the final result.

if (location.contains(",")) {
String city = location.substring(0, location.indexOf(",")).trim();
String state = location.substring(location.indexOf(",")).trim();
}
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • The definition of "trim" isn't valuable here since the OP has *some* idea as to what purpose it serves. Rather, why don't you explain why you put the trim operation *there*? – Makoto Sep 21 '15 at 06:03
  • Thanks a lot just did it and it worked, used trim on the outside. – user1591668 Sep 21 '15 at 06:21
1

Trim the entire result. For example:

String city = (location.substring(0, location.indexOf(","))).trim();
String state = (location.substring(location.indexOf(",") + 1)).trim();
Makoto
  • 104,088
  • 27
  • 192
  • 230
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
0

Use

String state = location.substring(location.indexOf(",") + 1).trim();

Instead of

String state = location.substring(location.indexOf(",".trim()) + 1);

That should work.

Adithya Rao
  • 165
  • 1
  • 8