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 :S

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);

}

Recommended Answers

All 10 Replies

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.

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);

}

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 .

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:

boolean b=true;
while(b) {
  String s=bReader.readLine();
  if (s==null) {
    break;  //file has ended
  }
}
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:

boolean b=true;
while(b) {
  String s=bReader.readLine();
  if (s==null) {
    break;  //file has ended
  }
}

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

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.

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.

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;     
   }

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 ...

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

u can still use the tokenizer class and the nextToken function if u like. Just read the values from nextToken into an array, then display the array from the back, by starting at array size and iterating down to zero

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.