• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

IO using FileWriter

 
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys I am getting strange and unexpected the data gets written to the file locations.txt with no problem but when I try to write to directions.txt it does not seem to work,I open the textfile and it has no text inside it unlike locations.txt but the very strange thing is I complied it and it did work at some stage but when I recompiled it it did not appear I tried undoing the changes to the program but still nothing appears any reason why this is hapening?

 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I think I know whats wrong I commented out the some of the locations.put code but that still brings up the question why does locations.txt still have the data in it when I recompile the program yet directions.txt does not
 
Master Rancher
Posts: 3876
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when I recompile the program


Recompiling the program should not cause any changes to other files on your PC.  Files get changed when a program is executed.
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Norman I'm not sure what the problem was but it worked out BUT now I am getting an exception I did some troubleshooting and isolated the line of code which is causing the error once I delete this line of code everything works fine but the problem is I need that line of code for my program to work



the line of code that cause the error is on 55 and it's
int destination = Integer.parseInt(dest);

here is the stackTrace,I'm not sure how to fix this exception


Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NumberFormatException: For input string: " 1"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Locations.<clinit>(Locations.java:55)
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok guys I found the solution when I was written to my output I included a space so when I tried to convert 1 to an interger 1 had a space before it and that space was included so the next logical question how do you prevent something like that happening apart from making sure there is no spaces?
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
without using the trim() method
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several ways of handling this sort of thing but trim() is by far the easiest. Is there a particular reason you don't want to use trim()?
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether you use scan.nextInt() or Integer.parseInt(dest) you will need to surround the code in its own try/catch block because you cannot be sure that the file will always have a valid integer.

Other notes:

* Do you need to use scan.skip(scan.delimiter());?  I thought Scanner skips the delimiter automatically.
* Have you tried a try-with-resources block?  You don't need a finally block if you do.
 
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible to avoid the exceptions with
if (myScanner.hasNextInt()) ...
but that can make for really awkward code. When reading from a text file it is necessary to know its format, so your nextInt() calls actually find text representing an int.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic