• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception error? Please help, frustrating

 
Matt Hazan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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.



 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would really help to see the full stack trace of your exception.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Okay, so I copied and pasted the exception details:

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11865
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • 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)
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote: . . . Manager: Good. We have a workaround.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic