import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; class Lese{ public static void main(String[] args){ int[] tallene = new int[9]; //Lager et tomt array med 9 plasser try{ les(tallene, "tall.txt"); //les() kaster eventuelle FileNotFoundException opp til oss her! } catch(Exception e){ //Haandterer potensiell FileNotFoundException System.out.println(e); System.out.println("Her fant vi ikke filen gitt...."); } Boolean foerste = true; System.out.print("["); //printer ut hvert tall i tallene for(int tall : tallene){ //For hvert tall i tallene, gjoer... if(!foerste) System.out.print(", "); System.out.print(tall); foerste = false; } System.out.print("]"); } public static int[] les(int[] tallene, String filnavn) throws FileNotFoundException, ArrayIndexOutOfBoundsException{ File filen = new File(filnavn); //Hvis vi IKKE finner filen, vil feilen genereres her. Men siden vi skriver "throws FileNotFoundException", //"kastes" feilen oppover. Altsaa til stedet det kalles. Scanner skannern = new Scanner(filen); int plass = 0; while(skannern.hasNextInt()){ //kjoerer saa lenge det neste elementet er et tall! tallene[plass] = skannern.nextInt(); plass += 1; } int a = tallene[70]; //ArrayIndexOutOfBoundsException! // System.out.println(a); return tallene; } }