ArrayIndexOut _ OfBoundsException

orchid

Grünschnabel
Hallo, bin noch java-newbie!
Ich habe hier diese Methode:
Ich kriege eine ArrayIndexOutOfBoundsException. Liegt glaub daran, dass z.B. field[0] zu lang ist. Da muss man doch irgendiw die Methode length() benutzen? Allerding weiß ich nicht wie und an welcher Stelle im Code ich das machen muss!
Hoffe mir kann hier jemand weiterhelfen.

Danke schon mal im voraus!

Code:
String[] fields;
public void readFile() throws ImportException {
		// checks whether the textfile can be open successfully
		if (selectedFile.canRead()) {
			// Read file, line by line
			BufferedReader br = null;
			
			try {
				br = new BufferedReader(
						new FileReader(selectedFile));
				for (lineno = 0;  br.ready(); lineno++) {
					//read one line of the textfile
					String oneLine = br.readLine();
					//each field delimited by semicolon
					fields = oneLine.split(";");
					System.out.println("Split");
					checkErrors();
				}
			} catch (FileNotFoundException e) {
				System.out.println("File could not be found");
				e.printStackTrace();
			} catch (IOException e) {
				System.out.println("Probleme beim Einlesen");
				e.printStackTrace();
			} catch (ArrayIndexOutOfBoundsException e) {
				System.out.println("Pflichtfelder");
				e.printStackTrace();
			}  finally {
				try {
					if (br != null) {
						// Closing the BufferedReader closes the underlying FileReader as well
						br.close();
					}
				} catch (IOException e) {
					System.out.println("Cannot close the file");
					e.printStackTrace();
				}
			}
		} else {
			throw new ImportException(messages.getMessage(700));
		}
		
	}
 
Re: HILFE wg. ArrayIndexOutOfBoundsException

Moin,
schätze Dein br.ready() in der for-Klausel gilt länger als Du Zeilen im BufferedReader hast.
 
Re: HILFE wg. ArrayIndexOut _ OfBoundsException

hi,
weißt du vielleicht wie man das br.ready() einschränken kann?
 
Re: HILFE wg. ArrayIndexOut _ OfBoundsException

Mach es einfach anders:
Code:
BufferedReader eingabe = new BufferedReader(eingabeStrom);
while ((zeile = eingabe.readLine()) != null)
{
 // tu was du willst mit der zeile
}
 
Zurück