This site requires JavaScript, please enable it in your browser!
Greenfoot back
protocolUnknown
protocolUnknown wrote ...

2019/8/11

I need help with how to read a txt file in greenfoot

protocolUnknown protocolUnknown

2019/8/11

#
I want for the ability to read a text file saved locally in the project folder. I tried doing this by looking at examples online b/c I don't know how to do it myself but it never works. The purpose for reading the text file is so I can search for keywords in the text file, and then the code will look at and save the line of text just below that keyword in the text file. It will save it in a list variable.
danpost danpost

2019/8/11

#
protocolUnknown wrote...
it never works.
Please show what you tried. Then, when corrected, you might see where it all went wrong. You will only need to provide the format of the text in your file (with example).
protocolUnknown protocolUnknown

2019/8/11

#
The Message class in this code is just an actor that sets its own image to the text that is given as it's argument.
    public void displayNotes(){
        
        
       try{

          //Create object of FileReader
          FileReader inputFile = new FileReader(fileName);

          //Instantiate the BufferedReader Class
          BufferedReader bufferReader = new BufferedReader(inputFile);

          //Variable to hold the one line data
          String line;

          // Read file line by line and print on the console
          while ((line = bufferReader.readLine()) != null)   {
              if(line.equals("Question:")){

                  notebookList.add(line);
              }
          }
          //Close the buffer reader
          bufferReader.close();
       }catch(Exception e){
          //System.out.println("Error while reading file line by line:" + e.getMessage());                      
       }
   
        
        notes();
        
    }
    
    public void notes(){
        
         for(int i =0; i < notebookList.size(); i++){
            //world = (MyWorld) getWorld();
              Message m = new Message((String)notebookList.get(i));
             getWorld().addObject(m, 6, 6);
            
          }
        
        
    }
protocolUnknown protocolUnknown

2019/8/11

#
oh and this format of text: Question: What time did you witness the crime? Question:1 You said 'them'? How many of them were there? Response: That's not what the eyewitness report on our file says. We place 4 criminals in the bank on that day. Question: Is there anything about the criminals you saw that could help us identify them? +--------------------------------- Witness: John Doe Q: What time did you witness the crime? It's kind of hard to say, but I'd say around 9:00 PM was when I saw them come into the bank. R:
danpost danpost

2019/8/11

#
On a cursory glance, I noticed that all your Message objects created in the for loop are placed at the same coordinates (on top of each other). Also, if this code is actually within the Message class, that is a big problem (as you would be creating Message objects from within the class) Next, I noticed that you iterate through the lines looking for each occurrence of "Question:"; then, you add "Question:" to the list each time found (does not seem right). How is the code not working -- are you getting any type of error output (what is it) or something else (explain in detail)?
protocolUnknown protocolUnknown

2019/8/11

#
I was able to fix it by moving everything to the world class. I think the issue was something else and not with the txt file reading method I had. I was able to fix it though, so thank you
You need to login to post a reply.