1

I need to read the text from a .dat file that goes as such:

4
Mary 13.99
Ruth 22.04
Anne 12.39
Talor 18.34

I used a buffered reader that looks like this:

public class Tester{

    public static void main(String [] args){

       BufferedReader reader = null;

       try {
           File file = new File("C:\\Users\\hoguetm\\workspace\\practiceproblems\\beautiful.dat");
           reader = new BufferedReader(new FileReader(file));

           String line;

           while ((line = reader.readLine()) != null) {
               //System.out.println(line);
               //split will go here
               String[] str1Array =  line.split(" ");
               System.out.println(str1Array[0]);

               //works

               /*
               for (String retval: line.split(" ")){
                    System.out.println(retval);
               }
               */
           }

       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               reader.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }  

}

i need to add the numbers at the end of the lines except the 4 and print the sum out, but when i change [0] to [1] it says out of range. please help

2
  • It is saying it is out of range because the first line you see only has the string "4" in it. Commented Dec 5, 2013 at 15:51
  • Try printing out the line you read, and the full array you parsed from it. Check your data coming in is as you expect. Commented Dec 5, 2013 at 15:52

6 Answers 6

3

Don't split the first line. Do split second line onward.

reader.readLine(); //added this line

while ((line = reader.readLine()) != null){

}
Sign up to request clarification or add additional context in comments.

Comments

0

If your input file is guaranteed to be in the format you posted, you can simply do this:

reader.readLine();
while ((line = reader.readLine()) != null) {
    ...

This would cause your reader to consume the first line of the file and ignore it completely. That said, the file you've posted suggests that the first line indicates the number of lines which follow, implying that you should parse it and use a for loop. For example:

int nLines = Integer.parseInt(reader.readLine());
for(int i = 0; i < nLines; i++)
{
    line = reader.readLine();
    ...
}

That's based entirely on the small sample you've provided, though. More information on the dat file's specification would be useful.

5 Comments

Yes the format is guaranteed. How would I add all the values at [1] of the array?
@user3070681, Your code is largely fine, you just weren't accounting for the first line of the file. Double.parseDouble(str1Array[1]) should get you the value on the given line.
how would i go about summing all of the values?
how would i sum all the values?
@user3070681, Outside of your loop, declare a variable of type double, initialized to 0. Inside of the loop, add the parsed value of strArray[1] to your variable.
0

It's because the first line only has one field. Try this instead:

double total = 0;
while ((line = reader.readLine()) != null) {
           String[] str1Array =  line.split(" ");
           if(str1Array.length > 1) {
               System.out.println(str1Array[1]);
               total += Double.parseDouble(str1Array[1]);
           }
       }

Comments

0

The String.split(String) method discards all emtpy Strings at the end of the array as per API-Documentation. If you ned to get back fixed-size arrays use line.split(" ",-1) - but be prepared to handle empty Strings in the result.

Comments

0

1 is out of range for that first row. String.split() will return an array with 1 element (the entire line) for that first line, and then an array with 2 elements for all other lines.

What's the general format of this .dat file? Is it always a single number on the first line, then name/number pairs for all subsequent lines? If that's the case, I would suggest ignoring the first line (with an extra reader.readLine() call before the loop. Or can the single-number lines be peppered throughout the file? If that's the case, then I would suggest checking the length of the array each time through the loop, and only doing the add if the length is 2.

Comments

0

Try this

int count =Integer.parseInt(reader.readLine());
String line;
while (count>0) {
    line = reader.readLine();             

    //System.out.println(line);
  //split will go here

 String[] str1Array =  line.split(" ");
System.out.println(str1Array[0]);

   //works

           /*
           for (String retval: line.split(" ")){
                System.out.println(retval);
           }
           */
 count--;
       }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.