2016-11-07 75 views
-3

所以即時通訊嘗試從一個數組中轉換一個數字,這個數組既有字符串又有整數,但不斷出現這個錯誤。如果我嘗試分析它,我得到的錯誤,它不能轉換爲字符串,如果我嘗試直接使用數組變量,我得到它不能轉換成int ... 繼承人的代碼對象無法轉換

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

public class sav 
{ 
public static void main (String [] args)throws IOException{ 
    try{ 
int rows = 0; 
//String temp; 
int tempint = 0; 
Scanner file = new Scanner (new File("sav_duom.txt")); 
while (file.hasNextLine()) 
{ 
    rows++; 
    file.nextLine(); 
} 

file = new Scanner (new File("sav_duom.txt")); 
System.out.println(rows); 
Object[][] data = new Object[rows][5]; 
for(int i = 0;i<rows;i++) 
{ 
    String str = file.nextLine(); 
    String[] tokens= str.split(","); 
     int temp = data[i][4]; 
     //temp = temp.replaceAll("\\W",""); 
     //tempint = Integer.parseInt(temp); 
    for (int j = 0;j<5;j++) 
    { 

     if(temp > 5) 
     { 
     data[i][j] = tokens[j]; 
     System.out.print(data[i][j]); 
     if(j == 3){ 
     System.out.print(":"); 
     } else { 
     System.out.print(" "); 
     } 
     } 
    }  
    System.out.print(" \n"); 
} 
file.close(); 
     } 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 

} 
} 

回答

1

我得到的錯誤,它不能被轉換成字符串

你需要轉換爲存儲值不同的類型。

Stringint使用Integer.parseInt(String)

int number = Integer.parseInt("1234"); 

intString使用String.valueOf(int)

String s = String.valueOf(1234); 

另外,根據在文件中的數據的類型的聲明data陣列int[][]String[][]

+0

嗯,我先轉換爲字符串,然後轉換爲int,但現在我得到這個錯誤 java.lang.NumberFormatException:對於輸入字符串:「null」 at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at sav.main(sav.java:34) – H4rdas

+0

'對於輸入字符串:「null」'表示存在是文件中的一個空字符串...檢查你的數據,並在更新中讀取鏈接的API方法 –

+0

當我嘗試打印出數組元素,即時試圖轉換,它打印字符串罰款 – H4rdas