Computing.Net > Forums > Programming > java arrays and reading from file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

java arrays and reading from file

Reply to Message Icon

Name: shane69
Date: May 10, 2006 at 07:36:21 Pacific
OS: xp
CPU/Ram: p4, 256
Product: dell
Comment:

hello,

i am trying to read from a file and take certain lines from the file and store them in an array.

the program works fine if i store it in a variable but when i try to store it in an array it does not work. here is my code:

try
{

file1 = new FileInputStream(fileName);

in = new DataInputStream(file1);



for (int i = 1; i < 1000; i = i++)
{

names[i].equals(in.readLine());

for (int z = 0; i < 15; i++)
{
System.out.println(names[z]);
} // end for



for (int x = 1; x < 8; x++)
{
in.readLine();

} // end for



if (fileInput .equals(null))
{
System.out.println();
} // end if




} // end for



in.close();



} // end try


thanks in advance

shane



Sponsored Link
Ads by Google

Response Number 1
Name: Clicker
Date: May 10, 2006 at 09:44:44 Pacific
Reply:

Your problem is in the following line


names[i].equals(in.readLine());
change this line to
names[i] = in.readLine();

make sure you have your array declared as a String type

the equals() function does not assign the value from your input stream to names array. you are acutally checking to see if the value of names[i] == in.readline().
So basically you are doing and == on a string... since java is an object based language it does not support and == operation instead it provides and equal() member function.

hope this helps


0

Response Number 2
Name: shane69
Date: May 10, 2006 at 18:15:43 Pacific
Reply:

thank you your code helped.

but now i am trying to read in integers from the same file and im trying to put them into a different array. I think readLine() reads from the file all output as strings and i cant get them to store them into an array. Ive tried using parseInt to convert it but i dont think that works with arrays.

shane


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: java arrays and reading from file

Urgent find word from file inc hepl www.computing.net/answers/programming/urgent-find-word-from-file-inc-hepl/18320.html

Reading from a file www.computing.net/answers/programming/reading-from-a-file/19662.html

C++ random names from file www.computing.net/answers/programming/c-random-names-from-file/4753.html