Results 1 to 3 of 3
  1. #1
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default If string does not match a number of strings problem

    I'm putting the finishing touches to a program. I have put this as an else statement but have noticed that in that case, if the shape matches one of the first two options (circle, rectangle), the program works perfectly (works out the area of both) but then puts this final error message. With triangle it doesn't happen (so the else if obviously linking to the triangle if).

    I was wondering if there was a way to work out if a string does not equal pre-defined strings. I have worked out this much myself, and think it may work in the case of !shape.equalsIgnoreCase(circle), but how do I get it to check multiple times (for the three options)

    Java Code:
    if (!shape.equalsIgnoreCase(circle || rectangle || triangle)) //if shape string doesn't equal the shapes strings
    					{ //open if
    					JOptionPane.showMessageDialog(null, "I don't recognise the shape " + shape + " cannot work out the area."); //Print message
    					} //close if

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: If string does not match a number of strings problem

    Java Code:
    String shape = "triangle";
    		String circle = "circle", rectangle = "rectangle", triangle = "triangle";
    		if (!Arrays.asList(circle, rectangle, triangle).contains(shape.toLowerCase())) {
    			JOptionPane.showMessageDialog(null, "I don't recognise the shape "+ shape + " cannot work out the area.");
    		}
    or you can use a regex :D

    Java Code:
    		if (!Pattern.compile("circle|rectangle|triangle", Pattern.CASE_INSENSITIVE).matcher(shape).find()) {
    			JOptionPane.showMessageDialog(null, "I don't recognise the shape "+ shape + " cannot work out the area.");
    		}
    or shorter ->
    Java Code:
    	if (!shape.matches("(?i)triangle|rectangle|circle")) {
    Last edited by eRaaaa; 11-17-2011 at 04:55 PM.

  3. #3
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: If string does not match a number of strings problem

    Thanks! That's great!

Similar Threads

  1. Test if string *could* match regular expression
    By kjkrum in forum Advanced Java
    Replies: 11
    Last Post: 07-12-2011, 10:01 AM
  2. Find the index number of strings in an arraylist
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 03-24-2011, 03:25 PM
  3. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  4. How to match arrayList with a String?
    By Lund01 in forum Advanced Java
    Replies: 2
    Last Post: 10-14-2010, 02:07 PM
  5. String match to the linkedlist element
    By jboy in forum New To Java
    Replies: 3
    Last Post: 09-06-2009, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •