Results 1 to 3 of 3
- 11-17-2011, 05:37 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
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
- 11-17-2011, 05:45 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 785
- Rep Power
- 12
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."); }
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."); }
Java Code:if (!shape.matches("(?i)triangle|rectangle|circle")) {
Last edited by eRaaaa; 11-17-2011 at 05:55 PM.
- 11-17-2011, 05:49 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Test if string *could* match regular expression
By kjkrum in forum Advanced JavaReplies: 11Last Post: 07-12-2011, 11:01 AM -
Find the index number of strings in an arraylist
By africanhacker in forum New To JavaReplies: 3Last Post: 03-24-2011, 04:25 PM -
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 07:53 PM -
How to match arrayList with a String?
By Lund01 in forum Advanced JavaReplies: 2Last Post: 10-14-2010, 03:07 PM -
String match to the linkedlist element
By jboy in forum New To JavaReplies: 3Last Post: 09-06-2009, 08:02 AM
Bookmarks