• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Exception error? Please help, frustrating

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I am trying to create a program that reads from an input file specified by the user and writes to a file by a name that is specified by the user.

The program reads from an input file containing integer numbers that represent Fahrenheit temperatures and then converts these to their equivalent Celsius temperatures, and writes them to the output file.

I am using Eclipse so I placed the .txt file in the Project Folder aka the folder where SRC and BIN folders reside in.


So, my main problem is that my program does not get to the part of reading/writing to the files, it just collects user data and then throws an exception (It compiles).

Thanks in advance.



 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would really help to see the full stack trace of your exception.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Hazan wrote:...it just collects user data and then throws an exception...


It would be helpful to know what exception is thrown by what line.

But I'm guessing the Scanner constructor in line 15 needs more than a String in order to read a file.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, division of ints truncates to an int result, so multiplying by (5/9)...
 
Matt Hazan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so I copied and pasted the exception details:

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure which is line 34 in your code, but I still think the problem is in passing a String to the Scanner constructor.
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Hazan wrote:Okay, so I copied and pasted the exception details:


The exception there points to line 34 in your code, which (as Marc notes) does not correspond to the source code you posted at the top. Using my psychic abilities, I think it matches to line 23 in the code you posted.

It would appear that when looking at the string inputFileName, the Scanner cannot find a number. Given that inputFileName probably contains something like "input.txt", I would agree that it is probably not a number. (For fun, you could tell it that your input file is "95" and see if the program works.

You might want to take a look at the difference between Scanner(String source) and Scanner(File source)
 
Sheriff
Posts: 22209
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You check for hasNext() which checks if any data is available at all, then you retrieve the data using nextInt(). Apparently the next data is not an int.
 
Marshal
Posts: 72993
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using a Scanner to read, why not use a Formatter to write to your file?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Monkhouse wrote:...you could tell it that your input file is "95" and see if the program works. ...


Manager: Good. We have a workaround.
 
Campbell Ritchie
Marshal
Posts: 72993
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

marc weber wrote: . . . Manager: Good. We have a workaround.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic