This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 12994
217
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
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!