• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need some help with Exception handling  RSS feed

 
Joe Chermak
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings everyone!

This is my first time posting here, many times I can find the issues in my code but I am having tremendous difficultly with exception handling. I am very new to programming and it is going decently so far, but some concepts I have a hard time wrapping my head around.

Here's my problem:

The assignment I am working on requires me to get input from a file to calculate the third side of a triangle. The resulting side must then be sent to an output file. If the program comes across a bad data pair, it must catch the exception, print out "Error" or something similar for that pair to the output file, then continue on to the next until it reaches the end of the input file.

Current status:

I can get the program to read the input file, calculate the third side, and print it out (only to console now, I will switch it to print to a file later once troubleshooting is complete). The problem is that when it comes across the error, it catches it and then gets stuck in a loop where is prints out "Bad input" over and over and over.

Question: Where am I going worng? I have tried several ways to handle this but nothing works. Does the program need another method? Am I close to handling the exception so that it can go to the next pair(s) until it reaches the end of the file?

Code is below. Thanks in advance for any advice or help you can offer!



Some of the commented stuff was just me trying things. If I am not being clear enough with my question please let me know so I can clarify.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typical problem with using Scanner. When you get an error, you have to call nextLine() so you can skip to the next line of input. Otherwise, the scanner will keep trying to read the bad input which is why you get into an infinite loop.

Welcome to the Ranch!
 
Joe Chermak
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! I was stuck in the mindset that I needed to call hasNext/nextInt! I changed it to nextLine() and BAM! Worked perfect!

Thanks again!
-Joe
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!