Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 advanceshane

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

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

![]() |
![]() |
![]() |

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