wood burning stoves*
The moose likes Beginning Java and the fly likes Exception error? Please help, frustrating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception error? Please help, frustrating " Watch "Exception error? Please help, frustrating " New topic
Author

Exception error? Please help, frustrating

Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

It would really help to see the full stack trace of your exception.


[My Blog]
All roads lead to JavaRanch
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

By the way, division of ints truncates to an int result, so multiplying by (5/9)...
Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Okay, so I copied and pasted the exception details:

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Mar 28, 2003
Posts: 11464
    
  94

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)

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
If you are using a Scanner to read, why not use a Formatter to write to your file?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 13, 2005
Posts: 39044
    
  23
marc weber wrote: . . . Manager: Good. We have a workaround.
 
GeeCON Prague 2014
 
subject: Exception error? Please help, frustrating