Hallo ich weiß nicht was ich mit dieser Exception anfangen soll und wie ich jetzt dafür sorgen kann, dass in case 3 der Spielstand lädt falls einer vorhanden ist und das die andere Option kommt falls nicht. Ein paar Ansätze zum beheben würden mich freuen.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:1515)
at Board.<init>(Board.java:45)
at TreasuryHuntApp.loadGame(TreasuryHuntApp.java:117)
at TreasuryHuntApp.performAction(TreasuryHuntApp.java:90)
at TreasuryHuntApp.mainMenu(TreasuryHuntApp.java:31)
at TreasuryHuntApp.main(TreasuryHuntApp.java:20)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:1515)
at Board.<init>(Board.java:45)
at TreasuryHuntApp.loadGame(TreasuryHuntApp.java:117)
at TreasuryHuntApp.performAction(TreasuryHuntApp.java:90)
at TreasuryHuntApp.mainMenu(TreasuryHuntApp.java:31)
at TreasuryHuntApp.main(TreasuryHuntApp.java:20)
Java:
private void mainMenu() {
// TODO print main menu to the console. Let user select an option. (s. Aufgabe 4)
boolean exit= false;
printHeader();
while (!exit) {
printMenu();
int choice = getMenuChoice();
performAction(choice);
}
}
private void printHeader() {
System.out.println("+-----------------------------------+");
System.out.println("| Welcome to the |");
System.out.println("| TreasuryHuntGame |");
System.out.println("+-----------------------------------+");
}
private void printMenu() {
System.out.println("Bitte wählen Sie eine der folgenden Funktionen aus.");
System.out.println("(1) Spiel starten");
if (hasRunningGame()){ System.out.println("(2) Spiel fortsetzen");}
if (hasSavedGame()) {System.out.println("(3) Spiel laden");}
if (hasRunningGame()) {System.out.println("(4) Spiel speichern");}
System.out.println("(5) Spiel Beenden");
}
private int getMenuChoice() {
Scanner keyboard = new Scanner(System.in);
int choice = 0;
do {
System.out.print("Enter your choice: ");
try { choice = Integer.parseInt(keyboard.nextLine());
} catch (NumberFormatException e) {
System.out.println("Ungültige Ausgabe. Bitte versuchen Sie es erneut");
}
if (choice < 1 || choice > 5) {
System.out.println("Ungültige Auswahl. Bitte verändern Sie ihre Eingabe.");
}
} while (choice < 1 || choice > 5);
return choice;
}
/**
* Lässt den Spieler seine Aktion wählen
* @param choice die Auswahl, welche der Spieler trifft als integer
*/
private void performAction(int choice) {
switch (choice) {
case 5:
System.out.println("Das Spiel wird beendet.");
System.exit(0);
break;
case 1:
System.out.println("Sie haben ein neues Spiel gestartet.");
startNewGame();
break;
case 2:
if (hasRunningGame())
{System.out.println("Sie wollen ein bestehndes Spiel weiterspielen.");
continueGame();}
else System.out.println("Diese Option ist momentan nicht verfügbar.");
break;
case 3:
if (hasSavedGame()) {System.out.println("Spielstand lädt");
loadGame();}
else System.out.println("Diese Option ist nicht verfügbar.");
break;
case 4:
if (hasRunningGame()) {System.out.println("Ihr Spiel wird gespeichert.");
saveGame();}
else System.out.println("Diese Option ist nicht vergübar.");
break;
default:
System.out.println("Unbekannter Fehler ist aufgetreten.");
}
}
Code:
private boolean hasSavedGame() {
return saveFilePath.toFile().exists();
}