Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading ints from file

Status
Not open for further replies.

skarosi

Programmer
Jan 25, 2004
140
GR
hi all,
i guess that u have seen this kind of question hundreds of times, but i cant find anything on the search so here it is.
I want to save some numbers on a file, and then access them.
i have saved the numbers using PrintWriter, and it comes out just fine when i open the file. The problem is that i cant open them
i have used a line for each number, so i nead something like readln, or readLine. i found the BufferedReader, but i am not sure how to use it, let alone that this can only read lines of string, and i cant change them to int.
Thanks
 
Code:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("file.txt")));
String line = "";
int iLine;
while ((line = br.readLine()) != null) {
  iLine = new Integer(line).intValue();
  System.out.println(iLine);
}

Without knowing how you data is in the file, this may or may not work, but it will work if your file is like :

Code:
1
2
3
4
5

BTW, Its considered polite in these forums to acknowledge that a solution has worked for a question you have posed ... a simple "thanks, that worked" is enough. This lets other people searching this site know that a solution works for that particular problem. (I refer to your previous int array question).

Cheers

--------------------------------------------------
Free Database Connection Pooling Software
 
thanks for ur reply sedj, but this doesnt seem to work. i get these errors:
C:\Documents and Settings\Ilias\My Documents\DESERTE\V103_DECODE_WITH_A\Gallager2.java:244: incompatible types
found : java.lang.Integer
required: int
iLine=new Integer(line.inValue());
^
C:\Documents and Settings\Ilias\My Documents\DESERTE\V103_DECODE_WITH_A\Gallager2.java:245: cannot resolve symbol
symbol : method inValue ()
location: class java.lang.String
iLine=new Integer(line.inValue());

I import these in the class:
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
do i need something extra?
thanks
 
My code was :

Code:
 iLine = new Integer(line).intValue();

You have copied it incorrectly - you have :

Code:
iLine=new Integer(line.inValue());

which is not the same ...

--------------------------------------------------
Free Database Connection Pooling Software
 
yeap, that did it. cheers mate. sorry for the truble
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top