Solved

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

Posted on 2006-07-21
7
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 76
login form jsp example 2 70
Problem to Alipay 10 94
Java program running SQL query 5 62
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

737 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