2016-10-21 11 views
-1

これは私が作成したコードの一部です。 リーダーは最初の文字列 "2"を読み取ったようですが、何らかの理由で整数に変換していません。その上Java NumberFormat読み取り中にエラーが発生しました

2 8 
6 9 
4 10 

と:

public void fileinput2() 
{ 
try 
{ 
     BufferedReader file=new BufferedReader(new FileReader("ddv.txt")); 
     try 
{ 
      while((line=file.readLine())!=null){ 
       String[] s=line.split("\\t+"); 
       int firstindex=Integer.parseInt(s[0].trim()); 
       int secondindex=Integer.parseInt(s[1].trim()); 
            adj[firstindex-1][secondindex-1]=1; 
       adj[secondindex-1][firstindex-1]=1; 
            /*for(int i=0;i<s.length;i++) 
            {System.out.println(s[i]); 
            int x=Integer.valueOf(s[i].trim()); 
            }*/ 

       } 
     } catch (NumberFormatException | IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

DDVはこのように書きテキストファイルです。 @kayKayが述べたように

私はこのエラー

java.lang.NumberFormatException: For input string: "2" 
at  
    java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 

at java.lang.Integer.parseInt(Integer.java:580) 
at java.lang.Integer.parseInt(Integer.java:615). 

を得るしかしおかげで私を助けてくださいに。:)

+0

で行を解析することができ、あなたのファイルに目に見えないUTF-8プレフィックス

では、httpを見て://stackoverflow.com/a/3762377/4626402 – Abhishek

答えて

0

変更区切り、あなたは再び行を読み取ろうとしています。私はあなたがすべきではないと思う?

public static void main(String[] args) { 
try { 
    br = new BufferedReader(new FileReader(theFile)); 
    String line = null; 

    while ((line = br.readLine()) != null) { 
     String[] aLine = line.split("\t"); // Also as kaykay mentioned change /t to \t 
     //br.readLine(); // You are reading the line again - Comment it out 
     numLine.add(aLine); 
    } 
} catch (IOException e){ 
} finally { 
    try { 
     br.close(); 
    } catch (Exception e) { 
    } 
} 

for (int i=0; i < numLine.size(); i++){ 
    for (int j = 0; j < numLine.get(i).length; j++){ 
     System.out.print(numLine.get(i)[j] + " "); 
     // if (!((numLine.get(i)[j]).equals("\t"))){ 
     intList.add(Integer.parseInt(numLine.get(i)[j])); 
    } 
System.out.println(); 
} 

タブ文字は\ tではなく、/ t(line.split( "\ t");)で表されます。さらに、読み取った各行が実際に整数である妥当性チェックを追加する必要があります

0

実際の "2"ではなく、 "2"の前に印字不可能なシンボル\ uFEFFがあります。それはあなたがファイルの最初の文字を分析したり、単に怠惰s.replace(「\ uFEFFを」、「」)を追加または正規表現

関連する問題