We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,567 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Compare a String to Multiple Strings in java

Hi All

I am new to java. I am facing a problem
I want to compare a string to a set of strings,
For example there is variable string x, If the value String X matches to String A , String B, String C then run the particular code otherwise do not run anything and come out of the loop.

My Code is:

    if((!X.equals("IND"))&&(!X.equals("AJD"))&&(!X.equals("BE"))){
    if (X.equals("IND")) {

        try {
            System.out.println("Form Type dropdown contains Individual");
        } catch (AssertionError e) {
            e.printStackTrace();
        }
    }

    if (X.equals("ADJ")) {
        try {

            System.out.println("Form Type dropdown contains Adjuster");
        } catch (AssertionError e) {
            e.printStackTrace();
        }
    }

    if (X.equals("BE")) {

        try {

            System.out.println("Form Type dropdown contains BE");
        } catch (AssertionError e) {
            e.printStackTrace();
        }
    }
    }else{
        System.out.println("Drop Down value does not match Individual, BE, and Adjuster");
    }
}

But os not wroking as desired ....:(
Please help!!!
Thanks in Advance...

2
Contributors
2
Replies
40 Minutes
Discussion Span
5 Months Ago
Last Updated
3
Views
Question
Answered
myadav
Newbie Poster
8 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

simple use if...elseif ladder to get output.
Like:

if(a.equals(b))
{
}
elseif(a.equals(c))
{
}
elseif(a.equals(d))
{
}
else
{
}

i think its work.

jalpesh_007
Posting Whiz
328 posts since Sep 2010
Reputation Points: 4
Solved Threads: 36
Skill Endorsements: 3

Yeah ...Its working .....Great !!!!
Thank You soo muchh

myadav
Newbie Poster
8 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by jalpesh_007

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1102 seconds using 2.67MB