简体   繁体   English

无法将int转换为String,/无法将String转换为int [] []

[英]cannot convert int to String, / cannot convert String to int [][]

Once again I am racking my brain as the error messages 作为错误信息,我再次绞尽脑汁
keep coming. 不断涌来。 I am learning how to work with arrays - both regular and multi-dimensioned. 我正在学习如何使用数组-常规和多维数组。 I am having problems a) filling the array with the sales data and also with a section where I get the "cannot convert String to an int". 我遇到问题a)用销售数据以及我得到“无法将String转换为int”的部分填充数组。 When I modify this array to have a string value, - I then get a flip flopped error,- "cannot convert int to String. Any assistance is appreciated. Thanks. 当我将此数组修改为具有字符串值时,-然后我收到一个翻转错误,-“无法将int转换为String。感谢您的帮助。谢谢。

    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]);
         }
     }
 } 
slsData[3][Months]

Months should be an integer here, to access the nth element of the 3rd array in slsData . 这里的Months应该是整数,以访问slsData第3个数组的第n个元素。 But the type of Months is String[] , so that doesn't make sense. 但是Months的类型是String[] ,所以这没有意义。 Maybe what you want to pass is the length of the Months array. 也许您想要传递的是Months数组的长度。 In that case, you would use slsData[3][Months.length] . 在这种情况下,您将使用slsData[3][Months.length]

But since slsData is an array of array of ints, slsData[3][12] that would be an int. 但是由于slsData是一个整数数组,因此slsData[3][12]可能是一个整数。 So you can't assign it to a variable of type String[][] . 因此,您不能将其分配给String[][]类型的变量。

Note that you should respect Java naming conventions. 请注意,您应该遵守Java命名约定。 Variables should have meaningful names, made of words (what do slsData and slsTablePP mean?), and should start with a lowercase letter (ie months , not Months ). 变量应具有有意义的名称,由单词组成( slsDataslsTablePP是什么意思?),并且应以小写字母开头(即, months而不是Months )。

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

Months is not an integer. 月不是整数。 You cannot directly convert an int array to a String array . 您不能直接将int数组转换为String数组 To convert an integer array to a string array take a look at Converting an int array to a String array 要将整数数组转换为字符串数组,请参阅将int数组转换为String数组。

String[]Months= new String [11];

The size of the above array should be 12. 以上数组的大小应为12。

Months is simply the name of your String array. Months只是您的String数组的名称。 In order to access individual values within the array you have to pass it the index of the value you want using the array operator [] . 为了访问数组中的各个值,您必须使用数组运算符[]将要传递的值的索引传递给它。

For instance: 例如:

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

or 要么

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

Obviously, this line is wrong: 显然,这一行是错误的:

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

Arrays are typed and you cannot magically convert int[][] to String[][] , furthermore, in Java, arrays are always indexed by int . 数组是类型化的 ,不能神奇地将int[][]转换为String[][] ,此外,在Java中,数组始终由int索引。 This slsData[3][Months] where Months is of type String[][] has no meaning. slsData[3][Months]MonthsString[][]类型String[][]没有意义。
Note, in some language, like Python, you have associative arrays ( Map and derived classes in Java) that can have an array syntax with string as index, eg 注意,在某些语言中,例如Python,您有关联数组(Java中的Map和派生类)可以具有以字符串作为索引的数组语法,例如

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

You have another issue when you declare the Months array. 声明Months数组时,您还有另一个问题。 In

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

The value [11] is the size of the array, not the last index, hence it should be 12 . [11]是数组的大小 ,而不是最后一个索引,因此它应该是12 In Java arrays are 0 indexed. 在Java中,数组的索引为0

Below I put a commented example of how you can match your different data arrays. 下面,我将提供一个注释示例,说明如何匹配不同的数据数组。

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);
        }
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM