Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将int转换为String,/无法将String转换为int[][]_Java_Arrays - Fatal编程技术网

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

Java 无法将int转换为String,/无法将String转换为int[][],java,arrays,Java,Arrays,当错误信息再次出现时,我绞尽脑汁 继续来。我正在学习如何使用数组-规则数组和多维数组。我遇到了以下问题a)用销售数据填充数组,还有一个部分我得到了“无法将字符串转换为整数”。当我修改这个数组使其具有一个字符串值时,-我得到一个触发器错误,-“无法将int转换为字符串。非常感谢您的帮助。谢谢 public class Sales{ public static void main (String []Args) { //a single

当错误信息再次出现时,我绞尽脑汁
继续来。我正在学习如何使用数组-规则数组和多维数组。我遇到了以下问题a)用销售数据填充数组,还有一个部分我得到了“无法将字符串转换为整数”。当我修改这个数组使其具有一个字符串值时,-我得到一个触发器错误,-“无法将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]);
         }
     }
 } 
公共类销售{
公共静态void main(字符串[]Args)
{   
//包含以下客户名称的单个维度数组
String[]name={“John Doe”、“Pete BuysAlot”、“Joe Stingy”、“Mary LikesOurStuff”};
//对于(int 0;i)
Months
在这里应该是一个整数,以访问
slsData
中第三个数组的第n个元素。但是
Months
的类型是
String[]
,所以这没有意义。可能您想要传递的是
Months
数组的长度。在这种情况下,您将使用
slsData[3][Months.length]

但是由于
slsData
是一个int数组,
slsData[3][12]
将是一个int。因此,您不能将其分配给
String[][]
类型的变量

请注意,您应该遵守Java命名约定。变量应该有有意义的名称,由单词组成(什么是
slsData
slsTablePP
的意思?),并且应该以小写字母开头(即
months
,而不是
months

月份不是整数。 不能直接将整型数组转换为字符串数组。 要将整数数组转换为字符串数组,请参见


上述数组的大小应为12。

Months
只是字符串数组的名称。要访问数组中的单个值,必须使用数组运算符
[]
将所需值的索引传递给它

例如:

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

String[]stringArray=新字符串[someList.size()]

对于(int i=0;i显然,这条线是错误的:

String [][] slsTablePP = slsData[3][Months];
数组是类型化的,您无法神奇地将
int[][]
转换为
String[][]
,而且,在Java中,数组总是由
int
索引。这个
slsData[3][Months]
类型的
Months
没有任何意义。
注意,在某些语言中,如Python中,您可以使用关联数组(
Map
和Java中的派生类),这些数组可以使用字符串作为索引的数组语法,例如

# python syntax
salesByMonth = {}
salesByMonth["jan"] = 2000;
salesByMonth["feb"] = 500; 
在声明
Months
数组时还有另一个问题

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

您的数组应该是12索引,而不是11索引。使用
String[]slsTablePP=slsData[3][Months]
,您打算做什么?您必须尝试使用String.valueOf(int)方法并将数组用作字符串数组。您好Ravi….我希望slsTablePP将由slsData中的数据填充。我的理解是,我的slsTablePP代码只是以数组格式创建表,但没有填充它。??但是
String[][]slsTablePP=slsData[3][11]呢
是否有效。我不这么认为?非常感谢大家花时间帮助我完成这项工作。我感谢你们与我分享你们的专业知识-我一定会研究建议的编辑,并密切关注代码更改。
String[] stringArray = new String[someList.size()]
for (int i=0; i<stringArray.length; i++) {
   stringArray[i] = someList.get(i);
   //some other stuff maybe
}
String [][] slsTablePP = slsData[3][Months];
# python syntax
salesByMonth = {}
salesByMonth["jan"] = 2000;
salesByMonth["feb"] = 500; 
String[] Months= new String [11]; // <= should be 12 !
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);
        }
    }
}