2015-04-04 92 views
1

再次我貨架我的大腦中的錯誤消息
滾滾而來。我正在學習如何處理數組 - 常規和多維。我有問題a)使用銷售數據填充數組,也使用「無法將字符串轉換爲int」的部分。當我修改這個數組有一個字符串值, - 然後我得到一個翻蓋失敗告終錯誤 - 「不能轉換INT爲String的任何援助表示讚賞感謝不能轉換INT爲String,/不能把字符串轉換成int [] []

public class Sales{ 

     public static void main (String []Args) 
     { 

     //a single dimension array containing the following customer names 

     String [] Names = {"John Doe","Pete BuysAlot","Joe Stingy","Mary  LikesOurStuff" } ; 
     // for(int 0;i<Names.length; i++)    
      // System.out.printl=n(Names[i]);} 
     //a single dimension array containing the names of //each     month    
     String[]Months= new String [11]; 

      Months[0] = " Jan "; 
      Months[1] = " Feb "; 
      Months[2] = " Mar "; 
      Months[3] = " Apr "; 
      Months[4] = " May "; 
      Months[5] =" June "; 
      Months[6] =" July "; 
      Months[7] =" Aug "; 
      Months[8] =" Sept "; 
      Months[9] =" Oct "; 
      Months[10]=" Nov "; 
      Months[11]=" Dec "; 




     // this next section creates the variables and data to create and initialize   
     // a two dimension array that reflects the purchases each person made per month. 
     //It will have the initial data in the following table 

    int[][]slsData = { {200,50,30,300,155,220,80,70,95,110,3,400}, 
       { 1200,2000,1500,900,1300,800,750,500,900,1200,1500,2000}, 
         {10,0,0,20,5,30,10,0,0,10,15,0}, 
         {500,100,200,400,600,200,150,155,220,336,43 ,455} 
           }; 

      String [][] slsTablePP = slsData[3][Months]; //here is where an error occurance is. [months] is a declared as a new string array but errors. 
      { 
       for (int row = 0; row <Names.length; row++) 
       for (int col = 0; col<Months.length; col++) 
       System.out.println(slsTablePP[row][col]); } 


      // array to hold sales figures totals by month 
       for(int x=0;x<mthlySales-1;x++) 
      System.out.println(Names[i] + mthlySls[x]); 
     } 
    } 
} 
+0

你的陣列應該是12指數沒有11 – Pratik 2015-04-04 07:00:49

+0

你有什麼打算與'的String [] [] slsTablePP = slsData [3] [月]'會發生什麼? – 2015-04-04 07:04:27

+0

您必須嘗試使用​​String.valueOf(int)方法並使用數組作爲字符串數組。 – Gopal00005 2015-04-04 07:23:05

回答

0
slsData[3][Months] 

Months應該是一個整數。在這裏,要訪問slsData中的第3個數組的第n個元素,但Months的類型是String[],所以沒有任何意義,也許你想要通過的是Months數組的長度,那樣的話,使用slsData[3][Months.length]

但由於slsData是一個int數組數組,slsData[3][12]將是一個int數組。所以你不能將它分配給String[][]類型的變量。

請注意,您應該尊重的Java命名約定。變量應該有有意義的名字,做的話(什麼slsDataslsTablePP是什麼意思?),並應以小寫字母(即months,不Months)開始。

+0

但是String [] [] slsTablePP = slsData [3] [11];''它是否工作。我不認爲? – silentprogrammer 2015-04-04 07:08:27

0
String [][] slsTablePP = slsData[3][Months]; 

幾個月不是一個整數。 不能直接轉換int數組到字符串數組中。 要轉換的整數數組到字符串數組看看Converting an int array to a String array

String[]Months= new String [11]; 

上述數組的大小應12

+0

非常感謝大家,你花了這麼多時間幫助我。我很感謝您與我分享您的專業知識 - 我肯定會研究所提出的修改並密切關注代碼更改。 – TimC 2015-04-05 22:53:08

0

Months簡直就是您的字符串數組的名稱。爲了訪問數組中的單個值,必須使用數組運算符[]將它傳遞給所需值的索引。

例如:

String[] stringArray = new String[4]; 
String fourthString = stringArray[3]; 

String[] stringArray = new String[someList.size()] 
for (int i=0; i<stringArray.length; i++) { 
    stringArray[i] = someList.get(i); 
    //some other stuff maybe 
} 
0

很顯然,這條線是錯誤的:

String [][] slsTablePP = slsData[3][Months]; 

數組是類型,你不能神奇地轉換int[][]String[][],此外,在J ava,數組總是以int索引。這slsData[3][Months]其中MonthsString[][]類型已經沒有意義。
注意,在一些語言,如Python,你必須陣列(Map和派生類在Java中),其可具有一個陣列語法與字符串作爲指標,例如

# python syntax 
salesByMonth = {} 
salesByMonth["jan"] = 2000; 
salesByMonth["feb"] = 500; 

當您聲明Months數組時,還有另一個問題。在

String[] Months= new String [11]; // <= should be 12 ! 

[11]是數組,而不是最後的索引的大小,因此它應該是12。在Java數組中,索引爲0

下面我列舉了一個如何匹配不同數據數組的示例。

public class Sales{ 
    public static void main (String []Args) {  
     //a single dimension array containing the following customer names 
     String [] Names = {"John Doe","Pete BuysAlot","Joe Stingy","Mary LikesOurStuff" } ; 
     //a single dimension array containing the names of each month    
     String[]Months= new String [12]; // <= 12 is the size not the last index ! 
     Months[0] = " Jan "; Months[1] = " Feb "; 
     Months[2] = " Mar "; Months[3] = " Apr "; 
     Months[4] = " May "; Months[5] = " June "; 
     Months[6] = " July "; Months[7] = " Aug "; 
     Months[8] = " Sept "; Months[9] = " Oct "; 
     Months[10]= " Nov "; Months[11]= " Dec "; 

     // two dimension array that reflects the purchases each person made per month. 
     int[][]slsData = { 
       { 200, 50, 30, 300, 155, 220, 80, 70, 95, 110, 3, 400}, 
       {1200, 2000, 1500, 900, 1300, 800, 750, 500, 900, 1200, 1500, 2000}, 
       { 10, 0, 0, 20, 5, 30, 10, 0, 0, 10, 15, 0}, 
       { 500, 100, 200, 400, 600, 200, 150, 155, 220, 336, 43 , 455} 
     }; 

     // match the sales data with name & month through the _indexes_ 
     // in the arrays. Here iName [0, 3] and iMonth [0, 11] will 
     // give the sales amount for a (name, month): slsData[iName][iMonth] 
     for (int iName = 0; iName < Names.length; iName++) { 
      System.out.println("\nSales for "+Names[iName]); 
      int total = 0; // total (re)set to 0 for each name 
      for (int iMonth = 0; iMonth < Months.length; iMonth++) { 
       System.out.println(Months[iMonth] + slsData[iName][iMonth]); 
       total += slsData[iName][iMonth]; 
      } 
      System.out.println("Total sales for "+Names[iName] + ": "+total); 
     } 
    } 
}