One of my students is attempting to use FileReader.textToString to import text files into an ArrayList as was done in Lesson 6.7, option a.
In 6.7a the code reads as follows:
import java.util.ArrayList;
public class WebsiteRunner {
public static void main(String args) {
// Creates an ArrayList of articles
ArrayList<String> articles = new ArrayList<String>();
articles.add(FileReader.textToString("ramen1.txt"));
articles.add(FileReader.textToString("ramen2.txt"));
articles.add(FileReader.textToString("ramen3.txt"));
articles.add(FileReader.textToString("ramen4.txt"));
articles.add(FileReader.textToString("ramen5.txt"));
My student’s code reads as follows:
import java.util.ArrayList;
public class NLPRunner {
public static void main(String args) {
ArrayList<String> review= new ArrayList<String>();
review.add(FileReader.textToString("Cookie.txt"));
review.add(FileReader.textToString("Cake.txt"));
review.add(FileReader.textToString("IceCream.txt"));
The code in 6.7a runs perfectly fine, yet my student is getting a cannot find symbol error with respect to textToString. I have literally spent hours looking at her code trying to figure out what the problem might be to no avail. I have seen on the forums that there are known issues related to a custom version of FileReader that is inconsistently attached to levels in the CSA curriculum. Is this the case for 6.10? I haven’t been able to find anything related to this specific issue on the forums, but I can’t think of anything else that might be causing the issue.
1 Like
Hi Christopher,
I’m sorry you are having a hard time with the FileReader in this lesson! The information about how to use the FileReader class is in the instructions area on this lesson, see the screenshot at the bottom of this post.
From what you have here, you would want to do something that looks more like this:
articles = FileReader.toStringList(“ramen.txt”);
ramen.txt should be a txt file in the project that has Strings in it. Each line in the txt file will be put into a different index in the ArrayList. Using the FileReader in this way means that you will not need to use the add method to create the ArrayLists.
One other detail to note is that students will need to add in the import statement for the ArrayList class, which you have in your student’s code-- I just wanted to highlight this for other teachers.
Here is a link to a short program I created that uses a .txt file and creates an ArrayList from it using FileReader:
1 Like
Thanks for the assistance.
So I am correct that the FileReader in 6.10 is different from the FileReader in 6.7? If so, that is extremely irritating. I really don’t understand why it would make any sense to show students how to import text files one way in 6.7 and then prevent them from doing it that way in the unit project just three lessons later. It would be really helpful if code.org would just provide us with a single FileReader that works the same way across all of the lessons instead of constantly changing it.
In any case, using toStringList seems to be a partial solution, in that it allows for a single text file to be imported as an ArrayList, but the problem remains that my student has three text files that she wants to import into a single ArrayList. Is there a simple way to do this without the ability to use the textToString method the students were shown in 6.7?
Hi @crodgers, I may have a solution to merge the three ArrayLists into one list. Here is what I found:
Using List.addAll()
The addAll() method is the simplest way to append all of the elements from the given list to the end of another list. Using this method, we can combine multiple lists into a single list.
Merge arraylists example
ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c"));
ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("c", "d", "e"));
listOne.addAll(listTwo); //Merge both lists
Is this what you had in mind?
Best,
-Sam
Yes, I think that would indeed help. We already found an alternate workaround (she just copied the contents of each text file into a single file), but this is good to know for future reference. Is addAll() a method of the ArrayList class? I do not see it in the documentation, but perhaps I am not looking in the right place.
Hey @crodgers, I’m glad y’all found a work around. addAll() is part part of the ArrayList class. If you want to see the Oracle documentation you can find it here.
Best,
-Sam
1 Like
One of my students is having this same issue. We solved it by writing a FileReaderUtilities class.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderUtilities {
static String textToString(String filename){
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append(System.lineSeparator());
}
}
catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
}
And then using FileReaderUtilities in NLPRunner as follows:
import java.util.ArrayList;
import java.io.FileReader;
import java.io.IOException;
public class NLPRunner {
public static void main(String args) {
String fileContent = FileReaderUtilities.textToString("Gatsby.txt");
System.out.println(fileContent);
}
}
It’s frustrating to have to mock up solutions like this. My students want to learn how to write Java that works outside the Code.org Java environment. The more we have to jerry-rig code like this, the more they feel like Java on Code.org is just “Java-light.”
They have a similar complaint when they find out that the Painter class exists only on Code.org.
Hi @jonathan.olson, thank you so much for posting your workaround to the forum! I understand your frustrations with the Code.org platform being so contained. Many of our students really embrace the complexities of the Java language and want access to the entire library of commands. Once students understand the basics and they want to branch out to more professional IDE’s I encourage them to.
Part of Code.org’s pedagogical approach to their curriculum values are the materials and tools they provide. These materials were designed to support exploration and discovery by those without computer science knowledge so that students can develop an understanding of these concepts through “play” and experimentation.
Again, thank you for being a part of our community and bringing computer science to the students at your school!
Best,
Sam
2 Likes