2016-05-18 16 views
0

txtファイルを読み込み、配列に保存しようとしています。JAVA-ファイル1の読み込みエラー(エラー)

A B 5
A C 2
A D 4
.....

public class search { 
    public static void main(String[] args) throws ParseException { 
     try { 

      Scanner user_input = new Scanner(System.in); // for user input    
      System.out.println("Enter the file name: "); 
      String filename1 = user_input.next(); 

      File file = new File(filename1); 

      search bd = new search(); 
      Node[] nodes; 
      nodes = bd.getNodes(file); 
      bd.printNodes(nodes); 

     } 
     catch(Exception e) { 
      System.out.println("Error reading file " + e.getMessage()); 
     } 
    }  

    public Node[] getNodes(File file) throws IOException { 
     FileReader bd = new FileReader(file); 
     BufferedReader bufferReader = new BufferedReader(bd); 
     String line; 
     ArrayList<Node>list = new ArrayList<Node>(); 
     while ((line = bufferReader.readLine()) != null) { 
      String[] token = line.split(" "); // create string of tokens 
      list.add(new Node(token[0], token[1], Integer.parseInt(token[2]))); 

     } 
     bufferReader.close(); 

     return list.toArray(new Node[list.size()]); // converting list to array 
    } 

    public void printNodes(Node[] nodes) { 
     System.out.println("======================"); 

     for(Node node : nodes) { 
      System.out.println(node); 
     } 
     System.out.println("======================"); 
    } 

後、私は何のコンパイルの問題を持っていない

class Node { 

    String leftchild; 
    String rightchild; 
    int cost; 

    public Node(){ 

    } 

    public Node(String firstchild, String secondchild, int cost){ 
     this.leftchild = firstchild; 
     this.rightchild = secondchild; 
     this.cost = cost; 
    } 

    public Node(String firstchild, String secondchild) { 
     this.leftchild = firstchild; 
     this.rightchild = secondchild; 
    } 

    public ArrayList<String> getChildren(){ 
     ArrayList<String> childNodes = new ArrayList<String>(); 
     if(this.leftchild != null) 
     { 
      childNodes.add(leftchild); 
     } 
     if(this.rightchild != null) { 
      childNodes.add(rightchild); 
     } 
     return childNodes; 
    } 

    public boolean removeChild(Node n){ 
     return false; 
    } 

    @Override 
    public String toString() { 
     return leftchild +" "+ rightchild; 
    } 

} 

私のNodeクラスですが、次の は、txtファイルの形式がありますコードを実行すると、なぜわからない

読み取りエラーファイル1

として私にエラーを与えています。私は多くの点で自分のコードを変更しようとしましたが、どれも働いていませんでした。誰も私のためにこれを理解してもらえますか? ありがとう

+1

使用' e.printStackTrace():配列のlengthlengthとして次の権利を持っている場合

だからあなたの場合には、最初にテストする必要があります。 – Kayaman

+0

はいこれを使用して、エラーが「スレッド内の例外」メイン「java.lang.ArrayIndexOutOfBoundsException:1'」であることを発見しました 私は 'printNodes'メソッドに何か問題があると思いますか? – rose

+0

Googleをいつ利用できるかを推測する必要はありません。 – Kayaman

答えて

1

ArrayIndexOutOfBoundsException: 1が表示された場合は、テスト入力ファイルにスペースを含まない行が少なくとも1つあることを意味します。

は確かにあなたのコードでは、あなたは、スペースで区切られたすべてのトークンを抽出し、Stringの配列にそれらを置くString[] token = line.split(" ")を行うので、このエラーが発生した場合には、配列のlength1、そのようなあなたができないことであることを意味しtoken[1]へのアクセスは、1つの要素のみを含むarrayの2番目の要素を指しています。 ; `catchブロックで完全なエラーメッセージを表示するように

String[] token = line.split(" "); // create string of tokens 
if (token.length >= 3) { 
    list.add(new Node(token[0], token[1], Integer.parseInt(token[2]))); 
} 
+0

少し具体的にお願いしますか? – rose

+0

応答がより明確になるように改善されました –

+0

ああ大変申し訳ありませんが、私にチェックさせてください。 – rose

関連する問題