read info from txt file

Thread Solved

Join Date: Feb 2008
Posts: 10
Reputation: spywx2003 is an unknown quantity at this point 
Solved Threads: 0
spywx2003 spywx2003 is offline Offline
Newbie Poster

read info from txt file

 
0
  #1
Feb 24th, 2008
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);

}
Last edited by spywx2003; Feb 24th, 2008 at 7:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: read info from txt file

 
0
  #2
Feb 25th, 2008
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.

  1. well i have this print method but when ever i try 2 use it it give me an error illegal start of expression
  2. so any idea
  3.  
  4. private static void displayResults(String[] rooms, int size)
  5. {
  6. int size=rooms.length;
  7. JTextArea textArea = new JTextArea();
  8. textArea.setEditable(false);
  9.  
  10. // Print Array output text area:
  11. for ( int i = 0; i < size; i++ )
  12. textArea.append(rooms[i]+"\n");
  13.  
  14. // Display the Array via a message dialog box:
  15. JOptionPane.showMessageDialog(null, textArea);
  16.  
  17. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 10
Reputation: spywx2003 is an unknown quantity at this point 
Solved Threads: 0
spywx2003 spywx2003 is offline Offline
Newbie Poster

Re: read info from txt file

 
0
  #3
Feb 25th, 2008
well do u think that i dont know that about size but that how my lab instructor wants it so ...btw i did fix the problem already thx for ur help , so anyidea about the 1st method read file .
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: read info from txt file

 
0
  #4
Feb 25th, 2008
  1. FileReader reader=new FileReader(new File("filename"));
  2. BufferedReader bReader =new BufferedReader(reader);
  3.  
  4. String s=bReader.readLine();

The API says that readLine() returns null if the file has ended, so you can do something like this:
  1. boolean b=true;
  2. while(b) {
  3. String s=bReader.readLine();
  4. if (s==null) {
  5. break; //file has ended
  6. }
  7. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 10
Reputation: spywx2003 is an unknown quantity at this point 
Solved Threads: 0
spywx2003 spywx2003 is offline Offline
Newbie Poster

Re: read info from txt file

 
0
  #5
Feb 26th, 2008
Originally Posted by javaAddict View Post
  1. FileReader reader=new FileReader(new File("filename"));
  2. BufferedReader bReader =new BufferedReader(reader);
  3.  
  4. String s=bReader.readLine();

The API says that readLine() returns null if the file has ended, so you can do something like this:
  1. boolean b=true;
  2. while(b) {
  3. String s=bReader.readLine();
  4. if (s==null) {
  5. break; //file has ended
  6. }
  7. }
well thx for the 1st part...i will give it a shot , and about the 2nd code i think u got the question wrong ..well the code wht it should do it check that the data in the txt file are on this format NHB|c241|200 , also to check if the last part is less than 3000 which i have the idea to use stringtoknizer to get the last part of the string and parse into int and see if it less than 3000 or not ...so if u gt more ideas let me know and also if i foud smthin i ll share it
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: read info from txt file

 
0
  #6
Feb 26th, 2008
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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 57
Reputation: piers is an unknown quantity at this point 
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: read info from txt file

 
0
  #7
Feb 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 10
Reputation: spywx2003 is an unknown quantity at this point 
Solved Threads: 0
spywx2003 spywx2003 is offline Offline
Newbie Poster

Re: read info from txt file

 
0
  #8
Feb 26th, 2008
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;
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 10
Reputation: spywx2003 is an unknown quantity at this point 
Solved Threads: 0
spywx2003 spywx2003 is offline Offline
Newbie Poster

Re: read info from txt file

 
0
  #9
Feb 26th, 2008
Originally Posted by piers View Post
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.
thx man i will look at it but i already started using string Tokenizer , i got a question for u lets say i have string like this "HNB|b145|300" lets say i want to get the last value only "300"

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 ...
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: read info from txt file

 
0
  #10
Feb 26th, 2008
Piers said:
Use Split. Go to the java API and it will tell you how to use it.
split is a method of String class
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC