Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestIntegers {
- /**
- * Program: TestIntegers.java
- * Purpose: Remove non-integer tokens from a string.
- * Creator: Chris Clarke
- * Created: 13.01.2015
- */
- public static boolean findError(String token) {
- int n;
- boolean error=false;
- try {
- n = Integer.parseInt(token);
- } catch (NumberFormatException e) {
- error=true;
- } // try..catch
- return error;
- } // test
- public static void main(String[] args) {
- String testString = "1 2 three 4 5 6 seven 8 9 10";
- String result = "";
- String[] theStrings = testString.split(" ");
- int errorCount=0;
- for (String s: theStrings) {
- if (findError(s)) {
- System.out.println(s + " is not a number.");
- errorCount++;
- } else {
- System.out.println(s + " is a number.");
- result += s+" ";
- } // if
- } // for
- System.out.println("In the string \"" + testString + "\",");
- System.out.println("there were "+errorCount+" items which were not numbers.");
- System.out.println("They've been removed: "+result);
- } // main()
- } // class TestIntegers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement