mirror of
https://github.com/smyalygames/kahoot-challenge-2025.git
synced 2025-05-18 13:14:16 +02:00
refactor: remove the throw argument for open file, and add try catch
This commit is contained in:
parent
9b031e3da6
commit
0dbd6eb471
@ -10,25 +10,30 @@ public class ReadFile {
|
|||||||
* @param filename the name of the file with, only allowing a .txt extension
|
* @param filename the name of the file with, only allowing a .txt extension
|
||||||
* @return The contents of the file
|
* @return The contents of the file
|
||||||
*/
|
*/
|
||||||
public List<String> open(String filename) throws IllegalArgumentException, FileNotFoundException {
|
public List<String> open(String filename) {
|
||||||
// Check that the filetype is correct, otherwise, throw IllegalArgumentException
|
// Check that the filetype is correct, otherwise, throw IllegalArgumentException
|
||||||
if (!filename.endsWith(".txt")) {
|
if (!filename.endsWith(".txt")) {
|
||||||
throw new IllegalArgumentException("The filename is not a .txt format");
|
throw new IllegalArgumentException("The filename is not a .txt format");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open the file that was defined
|
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
Scanner reader = new Scanner(file);
|
Scanner reader;
|
||||||
|
try {
|
||||||
|
// Open the file that was defined
|
||||||
|
reader = new Scanner(file);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
// List for all the separate lines on the database
|
// List for all the separate lines on the database
|
||||||
List<String> data = new ArrayList<>();
|
List<String> data = new ArrayList<>();
|
||||||
|
|
||||||
// Go through all the lines in the file and append them to the list `data`
|
// Go through all the lines in the file and append them to the list `data`
|
||||||
while (reader.hasNextLine()) {
|
while (reader.hasNextLine()) {
|
||||||
String currentData = reader.nextLine();
|
String currentData = reader.nextLine();
|
||||||
data.add(currentData);
|
data.add(currentData);
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user