This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes loading an integer data file into an integer array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loading an integer data file into an integer array" Watch "loading an integer data file into an integer array" New topic
Author

loading an integer data file into an integer array

Bill Stanard
Greenhorn

Joined: Oct 10, 2005
Posts: 12
In preparation for a computing contest here in So. Florida, I'm trying to figure out a simple method to read a numerical data file into an array so that I can then massage the numbers in various ways. This has turned into a daunting task because of unicode issues. The data file looks like this

where 0 signals an end of file. I can get the file contents to print out just peachy (in the standard output, bottom output box, using JGrasp), but I cannot find a simple way to load this data file AS INTEGERS into an integer array.
I load the array as follows:

What loads is as follows:

This has been printed out with the following:

I understand that what is being loaded is unicode for 5 [53] followed by the equivalent of a new-line [13 10] followed by unicode for the next 10 integers, et cetera. What I would like to do is to populate the integer array with the integers from the data file.

Any suggestions?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I would do something like this.

The numbers are stored in a file called numbers.txt.





The output is

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11358
    
  16

i think what you want to do is read the file a line at a time, as a string. then use the Integer.valueOf() method to convert it to an Integer.

then you can either store that, or convert it to an int and store it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bill Stanard
Greenhorn

Joined: Oct 10, 2005
Posts: 12
Thanks, Keith... Looks good and compiles, but I get an error when I run it


Any suggestions?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Indeed, a blank line is not a number. So you need to test for that so you can skip the blank lines that apparently appear in the file. Before calling Integer.valueOf, you need to check if the String is "".


[Jess in Action][AskingGoodQuestions]
 
GeeCON Prague 2014
 
subject: loading an integer data file into an integer array