Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading file and putting in an array  RSS feed

 
Tim Treichel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having troubles getting my program to read from a file to read numbers and put them into an array for another use. This is what I have so far, but I do not get the correct results. Everything I read makes me think that it is correct, but of course it is not. Any help would be greatly appreciated. TimT.

public static void main(String[] arg)
throws IOException
{
mortcalc_25 themortcalc = new mortcalc_25();
themortcalc.setVisible(true);

try
{

File data= new File("data.txt");;
int fileSize= (int) data.length();
FileInputStream file= new FileInputStream(data);
byte[] data2 = new byte[fileSize];
for(int i=0; i <= 5; i++ )
{
data2[i] = (byte) file.read();
System.out.println(data2[i]);
}

file.close();
}
catch (IOException e)
{
System.out.println("I could not read the file");
}

}
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are the data in your text file? is it space delimeted?
 
Tim Treichel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how it is in the data.txt file:

5.5
120
6.0
240
7.0
360

Does it matter how it is put into the data.txt file? I am new to Java and I really do know much about it, and it is not the easiest thing to learn. However, I have not found any reason to give up.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you expect this program to do? How do the actual results differ from what you expect? Are there any compiler errors you need help resolving? What about run time errors (excpetions)? Answers to these questions are crucial for us to be able to help you.

With that said, I'll take a stab in the dark and try to explain what is ACTUALLY happening with the code you posted. I can only guess what you EXPECT to do with this program.

First of all, I assume that your input file is text rather than binary. This is your first problem. FileInputStream is used to read binary data one byte at a time. This means that the file.read() method returns a single byte from the file. Depending on the character set used for this file, you may need to call file.read() multiple times since a character can be multiple bytes. To help you deal with these details, you probably need to wrap the FileInputStream with another InputStream subclass like DataInputStream. DataInputStream will convert the characters in the file into the numerical values that they represent. It even has methods that return the primitive numerical types (such as double and int).

If you need help understanding how the Java I/O classes work, I suggest that you take a look at this section from Sun's Java Tutorial. Once you understand what classes are available, you should also use the Java API docs for the version of Java that you are using in order to quickly look up the details of a given class or method.

I hope this helps.

Layne

p.s. If you still need help, let us know. Just be sure to refer to the questions that I started this post with. The more detail you give, the more easily we can answer your questions.
 
Tim Treichel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. Basically the program is to read data from the text file and then put it into an array for further user within the program. Such as loan rate and term for a mortgage calculator. However, I did figure out how to put the data into two different arrays. I think that is good.

The problem now is that I cannot use the array outside the public static void main. So I tried to comboloans.addItem. Howwever, I get the error message:

non-static variable comboloans cannot be referenced from a static context

I know what it mean, but how do I get around it? I hope this helps out. Here is what I have.

public static void main(String[] arg)
throws IOException
{

mortcalc_25 themortcalc = new mortcalc_25();
themortcalc.setVisible(true);

try
{

File data = new File("data.txt");
FileInputStream file = new FileInputStream(data);
DataInputStream input= new DataInputStream(file);

int fileSize = file.available();
double [] aRate = new double[3]; // Interest rate of loan
double [] aTerm= new double[3]; // Term in years

for(int count = 0; count == 2; count++)
{
aRate[count] = Double.valueOf(input.readLine()).doubleValue();
aTerm[count] = Double.valueOf(input.readLine()).doubleValue();
comboloans.addItem(aTerm[count] // trying to setup combobox
+ " years at "
+ (aRate[count]));

System.out.println(aRate[count]);
System.out.println(aTerm[count]);
}
file.close();
}
catch (IOException e)
{
System.out.println("I could not read the file");
}

}
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Treichel:
Howwever, I get the error message:

non-static variable comboloans cannot be referenced from a static context


Where and how you declare a variable determines where and how you can access it. Check out this article from the Java Tutorial.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!