0

Hey I'm totally finished with my try-catch block, but have question. I want to count how many words and numbers a string contains. For this i want to use a try - catch block. I parse the String to an int, and if the output is false, I want to count the number of words within the catch block logic. But after the program enters the catch block, the program exits. I want to continue iterating through the loop following the caught error. How can I make this happen?

    Scanner odczyt = new Scanner(System.in);
    String zdanie = odczyt.nextLine();
    String[] podzdania = zdanie.split(" ");
    boolean exception = false;
    int numberword = 0;

    try {

        for (int i = 0; i < podzdania.length; i++) {
            Integer.parseInt(podzdania[i]);
        }

    } catch (NumberFormatException e) {

        numberword++;

    }
Vince
  • 14,470
  • 7
  • 39
  • 84
krysss
  • 21
  • 1
  • 5

1 Answers1

6

Move the try-catch block within the loop:

for (int i = 0; i < podzdania.length; i++) {
  try {
    Integer.parseInt(podzdania[i]);
  } catch (NumberFormatException e) {
    numberword++;
  }
}

I would avoid using exceptions as a flow control mechanism though. Check this StackOverflow question for alternatives to validate the string before actually parsing it.

Pedro
  • 1,032
  • 6
  • 12
  • I can catch up on this one that is much better :) – davidxxx Jul 22 '18 at 20:11
  • @davidxxx haha I almost feel bad that a trivial answer like this gets me 5 fast upvotes – Pedro Jul 22 '18 at 20:15
  • You should not :) It is maybe a basic one but your answer is clear and the high level advise at the end makes the answer nicer. – davidxxx Jul 22 '18 at 20:20
  • 2
    @davidxxx But it goes against the philosophy of Stack Overflow, which is to be a quality Q&A repository. Segregating relevant content across many posts leads to inflation of posts and makes content harder to find. However, Stack Overflow is a broken system, hence why these actions are ignored [when mentioned](https://meta.stackoverflow.com/questions/357021/reputable-people-keep-answering-duplicates-whats-the-solution). If people were rewarded for finding duplicates & pairing relevant content, Pedro wouldn't have to resort to this. – Vince Jul 22 '18 at 20:55
  • @VinceEmigh, I agree that a lot of questions get answered even if there is a (usually better) duplicate answer within one google search's reach. However, I don't think this is such a question because the problem is quite specific to the code in question (misplaced try-catch block) and a canonical answer wouldn't help OP. – Mick Mnemonic Jul 22 '18 at 21:13
  • 1
    @VinceEmigh "segregating" is not the right word. The information is not being kept apart. "Distributing", "scattering" or, well, "duplicating" would be appropriate words for what I think you are trying to say. – Andy Turner Jul 22 '18 at 21:15
  • @VinceEmigh, I agree about SO being a broken system. I hardly find the time to answer in SO and yesterday I marked two duplicates which I could have easily answered. In this specific case I thought the mistake in the question was pretty specific (as Mick Mnemonic pointed out) which is why I answered. For the more fundamental issue of validating numeric strings, I still redirected to another question with several good answers on the topic. – Pedro Jul 22 '18 at 21:33
  • @Pedro Noticed I edited the question title to make it less specific. This problem has been solved many times, yet finding duplicates for something like this isn't simple; people tend to describe the same problem in different ways. That's part of the issue. If you're interested, I have a couple meta posts addressing some of these issues. But as mentioned before: although duplicates interfere with site quality, they seem to increase site activity & traffic, so I don't expect this to be changed anytime soon... Hate to say, but a proper solution is probably isn't worth the investment to them... – Vince Jul 22 '18 at 22:02
  • @VinceEmigh, exactly, I'm also sure this issue has been posted several times but it's hard to find the dup in this case. Different people will phrase the same thing in very different ways. I think the simpler the question, the harder it will be to find the dup. Tough questions will generally be posted by people with experience in the given topic and the format of the question will be more uniform, making it easier to spot duplicates. I already read a couple of your posts in meta and found the discussions very interesting, but the problem is certainly not an easy one to solve. – Pedro Jul 22 '18 at 22:16