I do not understand how to print a random line from a text file. So far in my program i have been able to read the file but not to print out a random line
import java.util.Random;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class filereading {
public static void main (String args []) {
File file = new File("MyPhrases.txt");
int count=0;
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// int intLine = Integer.parseInt(line);
// Random random = new Random();
// String word = random.nextLine(line);
// System.out.println(line);
count++;
// System.out.println(count);
Random random = new Random();
int rint = random.nextInt(count);
System.out.println(count);
}
} catch (FileNotFoundException e) {
System.out.println("error");
}
}
}

Start a new topic
Add Reply




MultiQuote
| 


