2016-11-24 2 views
-1

Hey mein Java-Programm hat eine Scanner-Klasse in es und es wird nicht die Datei lesen. Es gibt mir die folgende Ausnahme "DayCalParser.java:21: Fehler: nicht gemeldete Ausnahme FileNotFoundException; muss abgefangen oder als geworfen werden" Ich denke, ich sollte eine Art von IOexception verwenden, aber nicht sicher, wo zu setzen, da dies nur eine Klasse ist und es gibt keine Hauptmethode. Es ist keine Ausnahme, sondern einen KompilierungsfehlerScanner-Klasse liest die Datei nicht

Ich habe auch ein Fehler keine return-Anweisung sagen

import java.util.*; 
import java.io.*; 
import java.lang.*; 

public class DayCalParser { 
private int i = 0; 
private String userdate = ""; 
private Scanner scan = new Scanner(System.in); 
private int day = scan.nextInt(); 
private int fday; 
Scanner fileScan = new Scanner(new File("cal.txt")); 
private ArrayList <String> dates = new ArrayList <String>(); 


private void datesFinder(int fday) throws IOException { 

    while (fileScan.hasNext()) { 
    String line = fileScan.nextLine(); 
    if (line.contains("DTSTART")) { 
    if (line.length() > 25) { 
    String year = line.substring(19, 23); 
    String month = line.substring(23, 25); 
    String dayofmonth = line.substring(25, 27); 
    userdate = (dayofmonth + "/" + month + "/" + year); 

    } 
    } 

    if (line.contains("SUMMARY")) { 
    String summary = line.substring(12, 13); 
    if (summary.equals(fday)) { 
    i = i + 1; 
    dates.add(userdate); 
    } 
    } 
    } //while 
} 

public String getDates(int fday) { 
    datesFinder(fday); 
    for (int j = 0; j < dates.size(); j++) { 
    String a = dates.get(j); 
    return a; 
    } 
} 
} 
+0

Was erwarten Sie mit diesem Code? – emotionlessbananas

+0

einen try-catch-Block hinzufügen there'public Zeichenfolge getDates (int ftay) { ** datesFinder (fday); ** für (int j = 0; j XtremeBaumer

+0

ich bin erwarte es, eine Datei von Daten zu scannen, durch jede Zeile zu gehen, das Datum zu bekommen und dann durch zwei getrennte Methoden zurück zu kommen –

Antwort

0

Sie unter Code überprüfen kann, auch ändern getDates() Funktion und Sie diese Funktion über jede anrufen Klasse.

import java.util.*; 
import java.io.*; 
import java.lang.*; 

public class Main { 
    private int i = 0; 
    private String userdate = ""; 
    private Scanner scan = new Scanner (System.in); 
    private int day = scan.nextInt(); 
    private int fday; 
    private ArrayList<String> dates = new ArrayList<String>(); 


    private void datesFinder(int fday) throws IOException, FileNotFoundException { 
     Scanner fileScan = new Scanner(new File("cal.txt")); 
     while (fileScan.hasNext()){ 
      String line = fileScan.nextLine(); 
      if (line.contains("DTSTART")){ 
       if (line.length()>25){ 
        String year = line.substring(19,23); 
        String month = line.substring(23,25); 
        String dayofmonth = line.substring(25,27); 
        userdate = (dayofmonth + "/" + month + "/" + year); 

       } 
      } 

      if (line.contains("SUMMARY")) { 
       String summary = line.substring(12,13); 
       if (summary.equals(fday)){ 
        i = i+1; 
        dates.add(userdate); 
       } 
      } 
     }//while 
    } 

    public String getDates(int fday){ 
     try { 
      datesFinder(fday); 
      for(int j = 0; j < dates.size(); j++) { 
       String a = dates.get(j); 
       return a; 
      } 
     }catch (IOException e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public static void main(String[] args) { 
     Main obj = new Main(); 
     obj.getDates((int) (new Date().getTime()/1000)); 
    } 
} 
0

gefunden. Der Konstruktor von Scanner wirft FileNotFoundException. Übertragen Sie die Initialisierung innerhalb des DayCalParser Konstruktor

public DayCalParser() throws FileNotFoundException { 
    fileScan = new Scanner(new File("cal.txt")); 
} 

Auch ändern Sie Ihre getDates() dazu:

public String getDates(int fday) throws IOException { 
    datesFinder(fday); 
    for (int j = 0; j < dates.size(); j++) { 
     String a = dates.get(j); 
     return a; 
    } 
    return null; 
}