| | |
reading from file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 0
I'm really knew to this programming thing, and need a little help. My objective is to read data from a file like
2 2
0 0 wwdd
0 1 wwxd
1 0 ddwd
1 1 ddww
I have read the file line by line, but I do not know how to store the individual values. The first two numbers represent the length and width of the block. The rest are just coordinates.
Any ideas? The code should be flexible in case the row and columns change.
2 2
0 0 wwdd
0 1 wwxd
1 0 ddwd
1 1 ddww
I have read the file line by line, but I do not know how to store the individual values. The first two numbers represent the length and width of the block. The rest are just coordinates.
Any ideas? The code should be flexible in case the row and columns change.
import java.io.*;
import java.util.*;
class FileRead
{
public static void main(String args[])
{
String record = null;
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("h.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)
{
String delims =" " ; // DEFINES CHARACTER BEING USED
StringTokenizer st = new StringTokenizer(strLine, delims);
int frow = Integer.parseInt(st.nextToken());
int fcol = Integer.parseInt(st.nextToken());
System.err.println(frow);
System.out.println(strLine);
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
} Store them where? Because you are already storing them in varables:
Where you want them to be stored? You can put these variables wherever you want. Perhaps create a new class for each line and put those values in the class and then store each class in a Vector
Java Syntax (Toggle Plain Text)
int frow = Integer.parseInt(st.nextToken()); int fcol = Integer.parseInt(st.nextToken());
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Apr 2008
Posts: 5
Reputation:
Solved Threads: 0
hi dakrous
i read you problem but there's something that i don't understand.
2 2
0 0 wwdd
0 1 wwxd
1 0 ddwd
1 1 ddww
Q. The 2 2 is the length and width of your block and the remaining is the input. Do you have only to print the remaining output or stored it in an array?????
well you started the code well but in the middle you mess it up.
first thing: while getting the frow and fcol, why did you put it in a loop, only once you have to get this, you have to put it outside the loop.
second thing: if you have only to print the output, then print it in the while loop or if u have to save it and then print it, create an array of the size of your block and do it there
some changes:
String delims =" " ; // DEFINES CHARACTER BEING USED
strLine = br.readLine();
StringTokenizer st = new StringTokenizer(strLine, delims);
int frow = Integer.parseInt(st.nextToken());
int fcol = Integer.parseInt(st.nextToken());
System.out.println("Length" +frow+"Width:"+fcol);
while ((strLine = br.readLine()) != null)
{
System.out.println(strLine); //or save it in the array here
}
hope that this helps.
i read you problem but there's something that i don't understand.
2 2
0 0 wwdd
0 1 wwxd
1 0 ddwd
1 1 ddww
Q. The 2 2 is the length and width of your block and the remaining is the input. Do you have only to print the remaining output or stored it in an array?????
well you started the code well but in the middle you mess it up.
first thing: while getting the frow and fcol, why did you put it in a loop, only once you have to get this, you have to put it outside the loop.
second thing: if you have only to print the output, then print it in the while loop or if u have to save it and then print it, create an array of the size of your block and do it there
some changes:
String delims =" " ; // DEFINES CHARACTER BEING USED
strLine = br.readLine();
StringTokenizer st = new StringTokenizer(strLine, delims);
int frow = Integer.parseInt(st.nextToken());
int fcol = Integer.parseInt(st.nextToken());
System.out.println("Length" +frow+"Width:"+fcol);
while ((strLine = br.readLine()) != null)
{
System.out.println(strLine); //or save it in the array here
}
hope that this helps.
•
•
•
•
Teacher wants us to use tokenize. I tried the split thing though and it worked, but he won't accept it.
•
•
•
•
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
![]() |
Similar Threads
- Reading from file, passing into function. (C)
- Help on a reading from a file programming (Java)
- Need help reading a file (C++)
- Reading a file into a Parallel Array (C++)
- problems with reading in file (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Error Message Concerning Reading File From A Drive (C++)
- reading a file into code (Java)
Other Threads in the Java Forum
- Previous Thread: how to print out an array in a message box
- Next Thread: requesting permission to ask someone a question on java
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class classes client code component css csv database eclipse ee error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia jvm key linked linux list loan loop map method methods mobile netbeans newbie objects oracle oriented output panel phone print printf problem program programming project projects recursion replaydirector reporting researchinmotion robot scanner screen se server service set size sms software sort sql string swing test threads time transfer tree ubuntu windows






