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

2020/4/19

Read a text file with Scanner class

1
2
Fargesia Fargesia

2020/4/19

#
Hi everyone! I want to read a text file with the scanner class like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import greenfoot.*;
import java.io.File;
import java.util.Scanner;
 
public class pion extends Actor
{
    int X, Y, timer=-1;
    String reponse;
     
     
     
    public void act()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        File fichier = new File("reponse.txt");
        Scanner scan = new Scanner(fichier);
        reponse=scan.nextLine();
        System.out.println(reponse);
But it says I haven't declared my file, how do I have to declare it then? Thanks guys!
danpost danpost

2020/4/19

#
What exactly is in the error output? (copy/paste all lines) An aside: What is the purpose of getting mouse information here?
Fargesia Fargesia

2020/4/20

#
Well I just didn't post the whole class code but I use mouse information later but here I just want to check that reading files actually works so I can move on to the next step but apparently it doesn't. The exact error is: Unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
danpost danpost

2020/4/20

#
Make sure the file is in your scenario folder and also make sure that line 15 above reflects the name precisely (upper/lower case matters).
Fargesia Fargesia

2020/4/20

#
Yeah that's the thing, it is in the folder and it's name is exactly "reponse.txt". That's why I don't understand why it's not working...
danpost danpost

2020/4/20

#
Fargesia wrote...
Yeah that's the thing, it is in the folder and it's name is exactly "reponse.txt". That's why I don't understand why it's not working...
In your windows explorer (file directory window), right click the file in your scenario folder and select Properties. Make sure that ".txt" is not actually ".TXT". (or just try it in your code)
Fargesia Fargesia

2020/4/20

#
doesn't change anything, still get the same error, plus it is ".txt"
danpost danpost

2020/4/20

#
Fargesia wrote...
doesn't change anything, still get the same error, plus it is ".txt"
Okay. Are you sure it is "reponse" and not "response" (with "s" as the 3rd letter)?
Fargesia Fargesia

2020/4/20

#
Yep I am sure it's exactly the same, I actually speak french that's why it's "reponse" and not "response"
danpost danpost

2020/4/20

#
Try removing lines 15 thru 18 and put them in a:
1
public pion() {...}
(plus any other reads and closing file)
Fargesia Fargesia

2020/4/20

#
Still get the same error at "new Scanner(fichier);" Looks like it just doesn't find the text file but I really don't get why
danpost danpost

2020/4/20

#
Ah. I think you still need to do what I stated in my last post. However, you will need to wrap everything inside "public pion()" with:
1
2
3
4
5
6
7
8
9
try
{
    ... // open, read and close file here
}
catch(exception e)
{
    System.out.println(e.getMessage());
    try { /** close file */ } catch(exception e2) {}
}
Fargesia Fargesia

2020/4/20

#
Should it look like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public pion()
    {
         try{
            File fichier = new File("reponse.txt");
            Scanner scan = new Scanner(fichier);
            reponse=scan.nextLine();
            System.out.println(reponse);
         }
         catch(exception e)
         {
            System.out.println(e.getMessage());
            try {} catch(exception e2) {}
         }
     }
Like this it says "cannot find symbol - class exception"
RcCookie RcCookie

2020/4/20

#
Exception in line 9 has to be capitalized as Exception is a class
RcCookie RcCookie

2020/4/20

#
In line 12 as well
There are more replies on the next page.
1
2