Unit 3 Social Good project - number of lines in files

How do you expect students to get the count of number of lines in the text files? In the previous lessons (lesson 9 for example) the number of lines in national parks is given to the student. I can show my kids how to write code to count the lines in a file, but it seems to be beyond what they’ve learned so far. What were you expecting? jr

@jranta - John, thanks for posting to the forum. I understand that you are inquiring about the Social Good Project at the end of Unit 3. Could you give me some more information so that I can do some research on your question? I am wondering where the students are asked to count the number of lines in the text file. I read through the Planning Guide a couple of times and did not see a reference to counting the lines in the text file, but I could have missed it.
I would like to help this clarified for everyone. Thanks.

Sure. In unit 3 lesson 11 the Filereader (for the Music option, for example) requires students to enter in numValues, which represents the number of lines in the text file, and the number of elements in the array. How do they know how many lines are in the file? In previous examples, numValues is handed to the student by you guys. It seems like you should have a method in filereader to read the number of lines in the text files and return that as a number to pass to numValues. No? jr

Here’s the code:

public int getIntData(int numValues) {
int values = new int[numValues];

@jranta - thanks for the clarification. Here’s what I am understanding. Your question is how do the students know what value to use as a parameter in the calling statement since it is not given.
image

I believe this value can be found by looking at the txt files. Each file has a numbered list.

In my opinion, this should be part of the instructions or at least a hint. I will make that suggestion.
Please let me know if this answers your question. Have a great day!

Looking at the actual file in an editor is not good coding practice. In the future it might be better to provide a more general solution, that works for any file (like the code below). In any case, now I understand what code.org expects. Thanks. jr

// to count lines in a file - substitute your file for nationalparks.txt
// create a new file object
try {
  File file = new File("nationalparks.txt");

  // create a Scanner object
  // associated with the file
  Scanner sc = new Scanner(file);
  int count = 0;
  // read each line and
  // count number of lines
  while(sc.hasNextLine()) {
    sc.nextLine();
    count++;
  }
  System.out.println("Total Number of Lines: " + count);

  // close scanner
  sc.close();
}
 catch (Exception e) {
  e.getStackTrace();
}

Thanks for sharing this John! I’m sure others will benefit from seeing the discussion on needing to put the number of elements in as a parameter in the calls for the FileReader class methods. If you would like to suggest that the curriculum team change the set up of the class, you can email support@code.org. They do take our suggestions and choose updates on a priority basis.

Thank you,
Lindsay

I’m not quite familiar with how Java works on code.org, but every character in a text file has to be represented by some “data code” (not sure how else to explain it). For most unicode characters, such as the typable ones found on your keyboard, this just the letter itself. However, some special characters have to be represented by a certain string. For a new line character, this code is "\n".
Would it be possible to find the number of lines in a text file by finding how many instances of \n there are, and adding 1 for the beginning line? Please let me know!

Letti, I posted the code above that counts the lines. Did you check that out? In effect, it counts new line characters, because it counts using nextLine() to read each line in the file. You can email me directly if you have more questions - jranta@sau39.org . jr