Solved

how to check if a String has one word or two words

Posted on 2006-07-21
7
244 Views
Last Modified: 2010-03-31
Hi

I have a string which have state name
like
Arizona
californinia
South carolina
North carolina

what I need is to check

if(state is one word{

get standard abbreviation

}
else
{

get first letter of both the words

}

Would anyone know how to do this

Any help will be greatly appreciated
0
Comment
Question by:huzefaq
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17154583
You could keep a Map of states, then

                  Pattern p = Pattern.compile("^(.).+\\b(.).+");
                  Matcher m = p.matcher(inputString);
                  if (m.matches()) {
                        String letterOne = m.group(1);
                        String letterTwo = m.group(2);
                        System.out.printf("%s %s\n", letterOne, letterTwo);
                  }
                  else {
                        String state = stateMap.get(inputString);
                  }
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17155064
you can try this too,

if(state.indexOf(' ') > 0) {
// The state has more than 1 words.
}
else {
}

This works efficiently if the state has words seperated by one/more spaces
0
 

Author Comment

by:huzefaq
ID: 17155108
CEHJ Thanks

But

 System.out.printf("%s %s\n", letterOne, letterTwo);

this line is giving error

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:huzefaq
ID: 17155125
Ajay-Singh

How can I get the first leter of both the words then
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17155210
>>System.out.printf("%s %s\n", letterOne, letterTwo);

You can delete that
0
 
LVL 2

Expert Comment

by:RoyalNepal
ID: 17155511
You can do like this...too.



if (state.equals (""))//if state string is empty dont do anything
      return;

state = state.trim(); //takes out the spaces from first or last index

if(state.indexOf(' ') != -1)
{
  //return abbrev..it has only one word
}
else
{
//it has two words....so to take out the first letter..
//eg...if it has "South carolina" it returns South
  int index = state.indexOf(' ');
  return state.substring(0,index+1);
}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17156150
:-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 51
how to add new optional parameter to JSP 1 49
ejb example issues 3 19
running on tomcat not jboss eap 7.0 3 17
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question