Hey Leute habe folgendes Problem: Ich möchte eine Methode schreiben, die ein Drehbuch als .txt-Dokument Zeile für Zeile einließt und anhand des jeweils ersten Wortes der Zeile zählt welche Rolle wie oft gesprochen hat.
Ein Auszug aus dem Drehbuch:
"KELLY Let go of my hair, you little psychopath!
BUD Die, commie bimbo!
Bud pretends to cut her throat with a toy knife.
PEGGY Now, Bud, I thought we talked about this before."
Hier mein Lösungsversuch:
Nun wird beim ausführen jedoch folgende Fehlermeldung ausgegeben:
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at scriptreader.countRedeanteil(scriptreader.java:30)"
Jemand eine Idee zur Lösung meines Problems?
Viele Grüße
Ein Auszug aus dem Drehbuch:
"KELLY Let go of my hair, you little psychopath!
BUD Die, commie bimbo!
Bud pretends to cut her throat with a toy knife.
PEGGY Now, Bud, I thought we talked about this before."
Hier mein Lösungsversuch:
Java:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class scriptreader {
public static void countRedeanteil(File file) {
if (!file.canRead() || !file.isFile())
System.exit(0);
BufferedReader in = null;
int Al=0;
int Peg=0;
int Steve=0;
int Marcy=0;
int Kelly=0;
int Bud=0;
int Buck=0;
try {
in = new BufferedReader(new FileReader(file));
String zeile = new String();
String Rolle = new String();
while ((zeile = in.readLine()) != null) {
Rolle = zeile.substring(0,zeile.indexOf(" ")); //Zeile 30 (FehlerQuelle)
switch (Rolle) {
case "AL":
Al++;
break;
case "PEGGY":
Peg++;
break;
case "STEVE":
Steve++;
break;
case "MARCY":
Marcy++;
break;
case"KELLY":
Kelly++;
break;
case"BUD":
Bud++;
break;
case"BUCK":
Buck++;
break;
default:
break;
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Al's Redeanteil: " +Al+"Peg's Redeanteil: "+Peg+"Steve's Redeanteil: "+Steve+"Marcy's Redeanteil: "+Marcy+"Kelly's Redeanteil: "+Kelly+"Bud's Redeanteil: "+Bud+"Buck's Redeanteil: "+Buck);
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
}
}
}
}
Nun wird beim ausführen jedoch folgende Fehlermeldung ausgegeben:
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at scriptreader.countRedeanteil(scriptreader.java:30)"
Jemand eine Idee zur Lösung meines Problems?
Viele Grüße