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