0

I need to replace a random word that I pick in the MovieList into underscore "___", I think about create a new method to handle it, the new method should be under the class guessMovies, and then I will call method under "object r" to replace every single word into underscore, but Java said it cannot find symbol ( it doesn't know what "r" is ). Please help.

Thank you


I put object r out of try/catch scope, and use this code

            String movieToReplace = MovieNames.get(r.nextInt(MovieNames.size()));
        System.out.println(movieToReplace);
        MovieList.replaceAll(movieToReplace, "_");

but seem like it still gives me the random movie name, not a underscore "_"

public class guessMovies {

private static Random randomGenerator;



public static void main(String[] args) throws IOException {

    String MovieList;
    ArrayList<String> MovieNames = new ArrayList<>();
    Random r = new Random();



    try (BufferedReader br = new BufferedReader(new FileReader("MovieList.txt"))) {

        while ((MovieList = br.readLine()) != null) {
            MovieNames.add(MovieList);
        }

        for(String movieName: MovieNames){
            System.out.println(movieName);
        }


        String movieToReplace = MovieNames.get(r.nextInt(MovieNames.size()));
        System.out.println(movieToReplace);
        MovieList.replaceAll(movieToReplace, "____");

    }

    catch(FileNotFoundException exception) {
        System.out.println("I cannot find your file");

    }
    //pick random movie
}

}

ERROR:

Error:(38, 14) java: cannot find symbol

symbol: method replaceAll(java.lang.String,java.lang.String) location: variable r of type java.util.Random

FZ-07
  • 5
  • 5

2 Answers2

0

Your problem is related to variable scoping. You want to use r inside the method you write, but your method is not in try/catch block. Just create r outside of the try/catch block and you are fine. Like this:

Random r; try (BufferedReader br = new BufferedReader(new FileReader("MovieList.txt"))) { r = new Random();

Similar question:

Why does a Try/Catch block create new variable scope?

I suggest you to read http://www.geeksforgeeks.org/variable-scope-in-java/

Btw class names should start with upper case letters.

Oguz Ozcan
  • 1,497
  • 13
  • 21
  • 1
    Pretty sure its an issue with this line `r.replaceAll("/[a-z0-9. ]/", "__");`, hes trying to call a string method on a `Random` object. – luckydog32 Oct 18 '17 at 20:28
  • Yes it is an obvious mistake but he is saying he fixed the code and error, so I wrote wrt to his comment. – Oguz Ozcan Oct 18 '17 at 20:36
  • 1
    I think he was just saying that he edited his original post to include the error, but I could be reading it wrong. – luckydog32 Oct 18 '17 at 20:38
  • as I know, "r" is a object, but how can I use a string method for it? do you have any ways to get a random movie name on the list and then replace it ? – FZ-07 Oct 18 '17 at 21:07
0

Right now you are trying to call a String method on a Random object with the line:

r.replaceAll("/[a-z0-9. ]/", "__");

Since Random has no method replaceAll() you get the error you are seeing.

I think what you are trying to do is replace your movie in the original String with underscores.

    Random r = new Random();
    String movieToReplace = MovieNames.get(r.nextInt(MovieNames.size()));
    MovieList.replaceAll(movieToReplace, "_____");

However this method isn't safe for all scenarios. For example if you have two movies in your list: ["titanic", "titanic 2: jacks revenge"] And you do replaceAll() on "titanic" then your string would be "_______ _______ 2: jacks revenge"

So a safer way to do this would be to loop through your movie and list and rebuild the string, excluding the random index you want to replace, and instead replacing it with underscores.

luckydog32
  • 909
  • 6
  • 13
  • I want to pick a random movie in the list, then replace a random movie with the underscore "__", so I can hide a random movie name. – FZ-07 Oct 18 '17 at 21:05
  • Just edited my comment with a different method to use. Give me a heads up if it didn't clear things up for you. – luckydog32 Oct 18 '17 at 21:07
  • hello sir, I edited the code above, but it still gives me a random movie name, not underscore. Do I need to create a method >replaceAll – FZ-07 Oct 18 '17 at 23:21
  • Yes well you are printing out a random movie with the lines: `String movieToReplace = MovieNames.get(r.nextInt(MovieNames.size())); System.out.println(movieToReplace);` – luckydog32 Oct 19 '17 at 22:45