| | |
read info from txt file
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 10
Reputation:
Solved Threads: 0
private static int readFile (String filename, String[] capacities)
the file whose filename is given as a parameter, and fills array of strings representing
capacities.
Error checking:
Each line of the file should be checked for the following errors. Lines of the file that contain
errors should not be put in the array (print them to the system console).
· The line should have exactly three fields
· The capacity muse be greater than zero and less than 3000
The test for validity should be done by the method isValidRoom described below. The returned
value of the readFile method is the number of rooms successfully put in the array.
anyone can help with that i dont know really how to start it
data that should be in txt is
NSB|C205|164
NSB|B141|50
PH|C213|40
NSB|B145|50
also i got another question about another method
well i have this print method but when ever i try 2 use it it give me an error illegal start of expression
so any idea
private static void displayResults(String[] rooms, int size)
{
int size=rooms.length;
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
// Print Array output text area:
for ( int i = 0; i < size; i++ )
textArea.append(rooms[i]+"\n");
// Display the Array via a message dialog box:
JOptionPane.showMessageDialog(null, textArea);
}
the file whose filename is given as a parameter, and fills array of strings representing
capacities.
Error checking:
Each line of the file should be checked for the following errors. Lines of the file that contain
errors should not be put in the array (print them to the system console).
· The line should have exactly three fields
· The capacity muse be greater than zero and less than 3000
The test for validity should be done by the method isValidRoom described below. The returned
value of the readFile method is the number of rooms successfully put in the array.
anyone can help with that i dont know really how to start it

data that should be in txt is
NSB|C205|164
NSB|B141|50
PH|C213|40
NSB|B145|50
also i got another question about another method
well i have this print method but when ever i try 2 use it it give me an error illegal start of expression
so any idea
private static void displayResults(String[] rooms, int size)
{
int size=rooms.length;
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
// Print Array output text area:
for ( int i = 0; i < size; i++ )
textArea.append(rooms[i]+"\n");
// Display the Array via a message dialog box:
JOptionPane.showMessageDialog(null, textArea);
}
Last edited by spywx2003; Feb 24th, 2008 at 7:21 pm.
Where do you get your error at the following?
And why do you have size as an argument since you are calculating it in the method. It won't matter what value it will have when you call the method since it will take a new value; the size of the array.
And why do you have size as an argument since you are calculating it in the method. It won't matter what value it will have when you call the method since it will take a new value; the size of the array.
Java Syntax (Toggle Plain Text)
well i have this print method but when ever i try 2 use it it give me an error illegal start of expression so any idea private static void displayResults(String[] rooms, int size) { int size=rooms.length; JTextArea textArea = new JTextArea(); textArea.setEditable(false); // Print Array output text area: for ( int i = 0; i < size; i++ ) textArea.append(rooms[i]+"\n"); // Display the Array via a message dialog box: JOptionPane.showMessageDialog(null, textArea); }
Check out my New Bike at my Public Profile at the "About Me" tab
Java Syntax (Toggle Plain Text)
FileReader reader=new FileReader(new File("filename")); BufferedReader bReader =new BufferedReader(reader); String s=bReader.readLine();
The API says that readLine() returns null if the file has ended, so you can do something like this:
Java Syntax (Toggle Plain Text)
boolean b=true; while(b) { String s=bReader.readLine(); if (s==null) { break; //file has ended } }
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Feb 2008
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
Java Syntax (Toggle Plain Text)
FileReader reader=new FileReader(new File("filename")); BufferedReader bReader =new BufferedReader(reader); String s=bReader.readLine();
The API says that readLine() returns null if the file has ended, so you can do something like this:
Java Syntax (Toggle Plain Text)
boolean b=true; while(b) { String s=bReader.readLine(); if (s==null) { break; //file has ended } }
I am not going to do your homework for you. I gave the code for reading lines from a file. Each line is stored in the String: String s=bReader.readLine();. What you do with the string is your job. Is not that difficult; use StringTokenizer, or split(), and then make the necessary checks.
I don't know what the assignments says, but you could create a class with 3 attributes. Every time you read a line, create a new object with values, the values you get from the line, and store that object in an ArrayList, or Vector.
I don't know what the assignments says, but you could create a class with 3 attributes. Every time you read a line, create a new object with values, the values you get from the line, and store that object in an ArrayList, or Vector.
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Feb 2008
Posts: 10
Reputation:
Solved Threads: 0
javaAddict am not askin u 2 do my homework i am just asking for wht do u think should be done , i dont want any codez, anyway take a look at this let me know wht do u think
private static int inputFromFile(String filename,String[] lines)
{
TextFileInput in = new TextFileInput("inputFile.txt");
// Read lines into array:
int i = 0;
int arraySize=lines.length;
String line = in.readLine(); // read first line in file
while (i < arraySize && line != null )
{
lines[i] = line;
line = in.readLine(); // read next line in file
i++;
}
return i;
}
private static int inputFromFile(String filename,String[] lines)
{
TextFileInput in = new TextFileInput("inputFile.txt");
// Read lines into array:
int i = 0;
int arraySize=lines.length;
String line = in.readLine(); // read first line in file
while (i < arraySize && line != null )
{
lines[i] = line;
line = in.readLine(); // read next line in file
i++;
}
return i;
}
•
•
Join Date: Feb 2008
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
don't use stringTokenizer cus that is being wiped out. Use Split. Go to the java API and it will tell you how to use it.
i not using nextToken(); which doesnt work quit well since it force me 2 get the 1st 2 values 1st to get the last part of string ...
Piers said: split is a method of String class
•
•
•
•
Use Split. Go to the java API and it will tell you how to use it.
Last edited by javaAddict; Feb 26th, 2008 at 2:35 pm.
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Need help reading a file (C++)
- Custom Class - File I/O (C++)
- Reading or writing a double into a bin file (C++)
- Passing txt files into a programand writeln info out (Pascal and Delphi)
- How to read end of line? (C++)
- Lines are being skipped in the input file. (Visual Basic 4 / 5 / 6)
- Seg Fault (C++)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the Java Forum
- Previous Thread: How to create a simple flat file login console application?
- Next Thread: replace string values with new one?
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






