Results 1 to 6 of 6
Thread: Reading file .txt
- 11-18-2010, 05:02 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
Reading file .txt
Good morning everyone, I'm having the following problem:
I'm trying to read data from a file qeu are organized as follows:
2 0 0 0
2 0 1 0
2 0 2 0
2 0 3 0
2 0 4 0
2 0 5 0
2 0 6 0
2 0 7 0
2 0 8 0
2 0 9 0
time
I just need to put each column into a vector, where only the first three columns, so I will have three vectors X, Y and Z. and stop to read when you see the word "time"
I will then result in the vectors:
X: 2 2 2 2 2 2 2 2 2 2
Y: 0 0 0 0 0 0 0 0 0 0
Z: 0 1 2 3 4 5 6 7 8 9
Could someone send me a solution?
Thank you.
- 11-18-2010, 05:10 PM #2
- 11-18-2010, 05:15 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
i would use only one vector/list. write a class which stores the three ints (x,y,z). use the scanner class to read the file, instantiate objects of that class, and store it in your vector/list.
the scanner class has methods like hasNextInt and nextInt, next or nextLIne -->
while(sc.hasNextInt()){ read the next three integer --> create an object --> save the object in your list}
whats about the fourth integer/column? skip it ?Last edited by eRaaaa; 11-18-2010 at 05:27 PM.
- 11-19-2010, 06:02 PM #4
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
Try this:
File fileName = new File("C:\\Lovelesh\\Temp.txt");
if(fileName.exists())
{
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
String line = bufferedReader.readLine();
Vector<String> a = new Vector<String>();
Vector<String> b = new Vector<String>();
Vector<String> c = new Vector<String>();
while(null != line && (!line.equalsIgnoreCase("time") || line != null)){
String[] temp = line.split(" ");
if(temp.length > 1){
a.add(temp[0]);
b.add(temp[1]);
c.add(temp[2]);
}
line = bufferedReader.readLine();
}
System.out.println(a);
System.out.println(b);
System.out.println(c);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
- 11-19-2010, 10:35 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
@lovelesh: I wonder if your post isn't taking away the fun the OP might obtain from figuring out how to write the code. "Read the file line by line, split each line into its components and add each of the first three to three Vectors" would be a nudge that's gentle enough not to push the OP right out of the picture.
But if you do post code, please use the code tags: put [CODE] at the start of the code and [/CODE] so that the resulting post is formatted and readable.
----------------------------------
Java Code://Vector<String> a = new Vector<String>(); // why not List<String> a = new ArrayList<String>(); // change ArrayList to whatever List implementation makes sense // given the specific requirements
Basically there's no magic way to know that the Vector list implementation *is* the most appropriate (and a strong possibility that it isn't). And even it were the most appropriate, declaring the variable as "List<String> a = new Vector<String>()" would be more flexible.
Java Code://while(null != line && (!line.equalsIgnoreCase("time") || line != null)){ // why not while(line != null && !line.equalsIgnoreCase("time")) {
This latter pair aren't equivalent. As written your condition will fail to recognise the "time" line as a stopping condition.Last edited by pbrockway2; 11-19-2010 at 10:39 PM.
- 11-20-2010, 04:16 AM #6
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
You are right, but this guy was looking for solution, that's why :-(
I will keep this in mind, no on :-)
----------------------------------
Thanks for this, I didn't review it before posting that ....my mistakeLast edited by lovelesh; 11-20-2010 at 04:19 AM.
Similar Threads
-
reading a file and writing to a file....help!!!!
By java_prgr in forum New To JavaReplies: 3Last Post: 07-26-2010, 06:53 PM -
File Reading
By nuggetman4 in forum New To JavaReplies: 1Last Post: 05-10-2010, 05:49 AM -
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Right use of file reading ?
By jurka in forum New To JavaReplies: 3Last Post: 08-27-2008, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks