View Single Post
  #3 (permalink)  
Old 12-06-2007, 12:28 AM
hardwired hardwired is online now
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Code:
String str = "9,10,11,12"; String isIn = " "; do { isIn = JOptionPane.showInputDialog("Please enter a number"); if(isIn == null) break; int x = str.indexOf(isIn); boolean confirmed = false; if(x != -1) { boolean hasLeadingComma = x > 0 && str.charAt(x-1) == ','; boolean hasTrailingComma = x < str.lastIndexOf(",") && str.charAt(x + isIn.length()) == ','; boolean isFirst = x == 0 && hasTrailingComma; boolean isLast = x > str.lastIndexOf(",") && hasLeadingComma; confirmed = isFirst || isLast || (hasLeadingComma && hasTrailingComma); } if(confirmed) { System.out.println("Found " + isIn + " at index " + x); } else { System.out.println(isIn + " not found, x = " + x); } } while(isIn.length() > 0);
Reply With Quote