it is probably a simple question. I have a problem with scanning a text file. I want to scan a text file and to show the message in a JOptionPane. It scans but it shows only the first line of my text file and then stops disregarding the other lines. If I can get a little help. Thank you very much! Here is my code:
File file = new File("src//mytxt.txt");
try{
Scanner input = new Scanner(file);
while(input.hasNext()){
String line = input.nextLine();
JOptionPane.showMessageDialog(null, line);
return;
}
input.close();
}
catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "File not found");
}
}
Using
while (input.hasNext()) { //
scan
and append each line to aString
variable
}`
scan
each line from the file and keep the whole text in aString
variable. Then useJOptionPane.showMessageDialog(null,op)
to show the whole text in a singleJOptionPane
.