0

For example if I have a text file showing different integers, but if it comes across a value where it has a letter for example, it would throw a NumberFormatException. I have seen many times where a try-catch statement would be used, but is there any other way to handle this exception besides that? Here is an example for a txt file called "data" (Note that there are three integers separated by whitespace)

545F6 6 100

12N45 A 50

would the following code work?

while (data.hasNextLine()){
    data.nextInt();
    if (!data.hasNextInt()){
        System.out.println("The number " + data.next() + " is invalid");
        data.next();
    }
}

I am beginner in Java so I was curious if there was another way to ignore the strings, and show that it is invalid if it does not return an integer.

  • Kinda depends on what you mean by "ignore the strings". If you're looking to ignore, say "545F6", there's definitely an approach to do it. I believe `nextInt()` will read up to "545", so it's not going to be immediately useful to you. – Makoto Feb 27 '18 at 22:16
  • What you want to happen? To read `545` out of `545F6 6 100` or to get `54576100` from it? – Frakcool Feb 27 '18 at 22:17
  • Possible duplicate of [Check whether a string is parsable into Long without try-catch?](https://stackoverflow.com/questions/2563608/check-whether-a-string-is-parsable-into-long-without-try-catch) – Ousmane D. Feb 27 '18 at 22:20
  • It's complicated. The devil is in the details. 0xcafebabe can be seen as an int, for example, and 0999 can be rejected, as not an proper octal number. You may test for "[0-9]*", well but optionally, it might have a minus sign. It might exceed the value space of ints by far, or just by 1. :) Not to mention Roman numbers like "MMXVIII" – user unknown Feb 27 '18 at 22:20
  • Possible duplicate of [Java String remove all non numeric characters](https://stackoverflow.com/questions/10372862/java-string-remove-all-non-numeric-characters) – Frakcool Feb 27 '18 at 22:32

1 Answers1

0

You might want to give a try to regular expressions and use them in simple loop that checks if in the given strings separated by whitespace characters, there are only digits present (I added some more explanations in comments):

    String a = "545F6 6 100";
    String b = "12N45 A 50";

    List<String> results = new ArrayList<>(); // here you will store matching numbers
    for(String str : a.split("\\s+")) { // for each String that you get after splitting source String at whitespace characters...
        if(str.matches("\\b[\\d]+\\b")) { //check if that String matches given pattern: word boundary-only digits-word boundary
            results.add(str); // it there is a match, add this String to results ArrayList
        } else {
            System.out.println("The number " + str + " is invalid");
        }
    }

    System.out.println("Valid numbers: " + Arrays.toString(results.toArray())); // just to print results
    results.clear();
    System.out.println();

    for(String str : b.split("\\s+")) {
        if(str.matches("\\b[\\d]+\\b")) {
            results.add(str);
        } else {
            System.out.println("The number " + str + " is invalid");
        }
    }
    System.out.println("Valid numbers: " + Arrays.toString(results.toArray()));

Output that you get from these loops:

The number 545F6 is invalid
Valid numbers: [6, 100]

The number 12N45 is invalid
The number A is invalid
Valid numbers: [50]

You might want to read more about how to use regular expressions in Java API documentation of the class Pattern here.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21