2013-02-25 3 views
-3

Я не могу напечатать для метода getLines, я делаю что-то неправильно здесь? Он не дает мне никаких ошибок при запуске программы, но когда я пытаюсь напечатать метод getlines, он дает мне ошибки.не удалось распечатать arraylist

Это дает мне этот erros, когда я пытаюсь напечатать метод getlines. Исключение в потоке "основного" java.lang.ArrayIndexOutOfBoundsException: 1 при dijkstra.Fileprocess.getLines (Fileprocess.java:37) при dijkstra.Fileprocess.main (Fileprocess.java:70)

 public class Fileprocess { 


    public static Scanner Reader(String FileName){ 

     //Pass a File Name to this method, then will return Scanner for reading data from that file 
     try { 
      return new Scanner(new File(FileName)); 
     } catch (FileNotFoundException ex) { 
      ex.printStackTrace(); 
      System.exit(1); 
      return null; 
     } 
    } 
    static ArrayList<Edge> getLines(ArrayList<Vertex> PointCollection) { 
     Scanner Input = Reader(Vertex.graph); 
     ArrayList<Edge> result = new ArrayList<Edge>(); 

     while(Input.hasNext()){ 
      String line = Input.nextLine(); 

      String[] arr = line.split(" "); 
      result.add(new Edge(Integer.parseInt(arr[0]), //index 
        getPointbyIndex(PointCollection,Integer.parseInt(arr[1])), //start 
        getPointbyIndex(PointCollection,Integer.parseInt(arr[2])), //end 
        Integer.parseInt(arr[3]))); //cost 
      } 
     Input.close(); 
     return result; 
    } 

    static ArrayList<Vertex> getPoints() { 
     Scanner Input = Reader(Vertex.airports); 
     ArrayList<Vertex> result = new ArrayList<Vertex>(); 

     while(Input.hasNext()){ 
      String line = Input.nextLine(); 
      result.add(new Vertex(line)); 
     } 
     Input.close(); 
     return result; 
    } 

    static Vertex getPointbyIndex(ArrayList<Vertex>PointCollection, int Index){ 
     for(Vertex p:PointCollection){ 
      if(p.getIndex() == Index){ 
       return p; 
      } 
     } 
     return null; 
    } 

public static void main(String[] args){ 
    System.out.println(getPoints()); 
    System.out.println(getLines(null)); 
    } 


} 

это файл для входного текстового файла (индекс, начало, конец, стоимость)

1 1 2 2 
2 1 3 1 
3 1 6 3 
4 1 7 3 
5 2 1 2 
6 2 3 1 
7 2 4 1 
8 2 5 2 
9 2 6 2 
10 3 1 1 
11 3 2 1 
12 3 4 1 


class Edge { 

    public Vertex start; 
    public Vertex end; 
    public double cost; 
    public int Index; 

// public final Vertex target; 
// public final int weight; 


    public Edge(double cost, Vertex end, Vertex start, int Index){ 

     this.start = start; 
     this.end = end; 
     this.cost = cost; 
     this.Index = Index; 
    } 
     public String toString(){ 
      String result = ""; 
      if(this.start != null && this.end != null){ 
       result = this.Index +","+this.start.Index+","+this.end.Index +","+this.cost; 
      } 
      return result; 
     } 


} 
+3

В чем проблема? Как мы можем помочь, если вы не сообщите нам, в чем проблема? – Doorknob

+0

это дает мне этот erros, когда я пытаюсь напечатать метод getlines. Исключение в потоке «основного» java.lang.ArrayIndexOutOfBoundsException: 1 \t на dijkstra.Fileprocess.getLines (Fileprocess.java:37) \t в dijkstra.Fileprocess.main (Fileprocess.java:70) – mon999

+1

Ну тогда, может быть, вы должны поставить это в вопросе! – Doorknob

ответ

0

Возможно, ваш файл содержит пустую или несовместимую строку?

вы можете исправить ошибку, используя:

String[] arr = line.split(" "); 
if (arr.length > 3) { 
    result.add(new Edge(Integer.parseInt(arr[0]), //index 
      getPointbyIndex(PointCollection,Integer.parseInt(arr[1])), //start 
      getPointbyIndex(PointCollection,Integer.parseInt(arr[2])), //end 
      Integer.parseInt(arr[3]))); //cost 
    } 
} 
+0

В этом файле нет пустых строк, но теперь он не дает мне ошибок после ввода кода ur, но могу ли я спросить, что делает if (arr.length> 3)? – mon999

+0

У вас есть ошибка, потому что вы пытались получить доступ к индексу в массиве, который не имеет этого индекса. Здесь вы пытаетесь получить доступ к массиву только в том случае, если этот индекс существует. – BobTheBuilder

+0

хорошо не дает мне ошибку сейчас, но я не уверен, как я могу распечатать файл, могу ли я использовать System.out.println (null) ;? – mon999