Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Need some help with Exception handling  RSS feed

 
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.
 
Sheriff
Posts: 12747
210
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
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!