Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.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数组,然后将int转换为*_Java_Arrays - Fatal编程技术网

Java 将字符串数组转换为int数组,然后将int转换为*

Java 将字符串数组转换为int数组,然后将int转换为*,java,arrays,Java,Arrays,我试着用一个由国家和手机数量组成的字符串数组,例如:秘鲁33。然后我想把这些数字转换成一个整数,然后用***替换整数来制作一个条形图 智利*************** public class GraphNumbers { int ROWS = 5; int COL = 2; int i, j; public GraphNumbers(){}; public String getArray()

我试着用一个由国家和手机数量组成的字符串数组,例如:秘鲁33。然后我想把这些数字转换成一个整数,然后用***替换整数来制作一个条形图

智利***************

public class GraphNumbers
    {
        int ROWS = 5;
        int COL = 2;
        int i, j;

        public GraphNumbers(){};

        public String getArray()
        {
        String[][] cellPhoneNumbers = new String[ROWS][COL];
            cellPhoneNumbers[0][0] = "Chile";
            cellPhoneNumbers[0][1] = "21";
            cellPhoneNumbers[1][0] = "Sweden";
            cellPhoneNumbers[1][1] = "11";
            cellPhoneNumbers[2][0] = "Peru";
            cellPhoneNumbers[2][1] = "33";
            cellPhoneNumbers[3][0] = "Bulgaria";
            cellPhoneNumbers[3][1] = "10";
            cellPhoneNumbers[4][0] = "Guatemala";
            cellPhoneNumbers[4][1] = "18";

            for (i = 0; i < ROWS; i++)
            {
                int phones = Integer.parseInt(cellPhoneNumbers[i][1]);
                for (j = 0; j < COL; j++)
                {
                    System.out.print(cellPhoneNumbers[i][j] + " ");
                }
                System.out.print("");
            }

        return "";
        }

}
瑞典******

public class GraphNumbers
    {
        int ROWS = 5;
        int COL = 2;
        int i, j;

        public GraphNumbers(){};

        public String getArray()
        {
        String[][] cellPhoneNumbers = new String[ROWS][COL];
            cellPhoneNumbers[0][0] = "Chile";
            cellPhoneNumbers[0][1] = "21";
            cellPhoneNumbers[1][0] = "Sweden";
            cellPhoneNumbers[1][1] = "11";
            cellPhoneNumbers[2][0] = "Peru";
            cellPhoneNumbers[2][1] = "33";
            cellPhoneNumbers[3][0] = "Bulgaria";
            cellPhoneNumbers[3][1] = "10";
            cellPhoneNumbers[4][0] = "Guatemala";
            cellPhoneNumbers[4][1] = "18";

            for (i = 0; i < ROWS; i++)
            {
                int phones = Integer.parseInt(cellPhoneNumbers[i][1]);
                for (j = 0; j < COL; j++)
                {
                    System.out.print(cellPhoneNumbers[i][j] + " ");
                }
                System.out.print("");
            }

        return "";
        }

}
但我犯了个错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 21
    at GraphNumbers.getArray(GraphNumbers.java:28)
    at CellPhoneGraph.main(CellPhoneGraph.java:8)
Java Result: 1 
我对此感到非常困惑,因此任何帮助都将不胜感激。多谢各位

我试着用手机在第二个for循环中打印行int来打印整数

System.out.print(cellPhoneNumbers[i][phones])
在这里,解析
cellPhoneNumbers[0][1]
后,您将得到整数
21
,这是
“21”
,您正试图访问
cellPhoneNumbers[i][phones]
,这显然是不绑定的,因为您的数组是
String[][]cellPhoneNumbers=新字符串[5][2]
所以对于
col>=2
它将被视为您的阵列的绑定外

System.out.print(cellPhoneNumbers[i][phones])//<---for phone=21
System.out.print(手机号码[i][phones])//
我试着用手机在第二个for循环中打印行int来打印整数

System.out.print(cellPhoneNumbers[i][phones])
在这里,解析
cellPhoneNumbers[0][1]
后,您将得到整数
21
,这是
“21”
,您正试图访问
cellPhoneNumbers[i][phones]
,这显然是不绑定的,因为您的数组是
String[][]cellPhoneNumbers=新字符串[5][2]
所以对于
col>=2
它将被视为您的阵列的绑定外

System.out.print(cellPhoneNumbers[i][phones])//<---for phone=21

System.out.print(cellPhoneNumbers[i][phones])//因为
phones
的值是
cellPhoneNumbers[i][1]
的整数值,而COL数组只有2,那么它肯定会失败

顺便说一句,使用System.out.println(…)在单独的行上打印,如果返回的都是“”,为什么要让方法返回一个字符串?使用一个void函数代替

要打印出
*
请尝试

for (int x = 0; x < phones; x++) {
    System.out.print("*");
}
System.out.println("");
for(int x=0;x
因为
电话的值是
手机号码[i][1]
的整数值,而COL数组只有2,那么它肯定会失败

顺便说一句,使用System.out.println(…)在单独的行上打印,如果返回的都是“”,为什么要让方法返回一个字符串?使用一个void函数代替

要打印出
*
请尝试

for (int x = 0; x < phones; x++) {
    System.out.print("*");
}
System.out.println("");
for(int x=0;x
您的第一个问题是,要以单独的行打印,您需要执行System.out.println(手机号码[i][j])(请注意println,而不仅仅是打印)

要打印出与呼叫代码相关的星星数,您需要在号码上循环并打印每个*而不是使用手机号码[i][phone]——使用“phone”作为数组索引,您引用的是第21个索引,这显然超出了范围

因此,要打印星星,请在第二个for循环中执行以下操作:

for (int k = 0; k < phones; k++) {
    System.out.print("*");
}
System.out.println(""); //this will provide a line break after the stars.
for(int k=0;k
您的第一个问题是,要以单独的行打印,您需要执行System.out.println(手机号码[i][j])(请注意println,而不仅仅是打印)

要打印出与呼叫代码相关的星星数,您需要在号码上循环并打印每个*而不是使用手机号码[i][phone]——使用“phone”作为数组索引,您引用的是第21个索引,这显然超出了范围

因此,要打印星星,请在第二个for循环中执行以下操作:

for (int k = 0; k < phones; k++) {
    System.out.print("*");
}
System.out.println(""); //this will provide a line break after the stars.
for(int k=0;k
在第一次迭代中,从
[0][1]
位置的数组值中解析名为
phones
int
。此值为
“21”
,因此您解析的
int
将具有值21

在这种情况下,这一行:

System.out.print(cellPhoneNumbers[i][phones]);
相当于做:

System.out.print(cellPhoneNumbers[0][21]);
该位置没有值,因为数组只有5行2列,因此出现了
ArrayIndexOutOfBoundsException


关于在单独的行上打印它们,您可以使用
System.out.println(“…”)
而不是
System.out.print(“…”)

在第一次迭代中,您从
[0][1]
位置的数组值解析名为
phones
int
。此值为
“21”
,因此您解析的
int
将具有值21

在这种情况下,这一行:

System.out.print(cellPhoneNumbers[i][phones]);
相当于做:

System.out.print(cellPhoneNumbers[0][21]);
该位置没有值,因为数组只有5行2列,因此出现了
ArrayIndexOutOfBoundsException


关于在单独的行上打印它们,您可以使用
System.out.println(“…”)
而不是
System.out.print(“…”)

使用
phones
作为索引是错误的,因为第二个维度可以是0或1。。。在您的示例中,您使用手机的值作为索引:21

您可以使用以下代码:

 for (i = 0; i < ROWS; i++) {
   System.out.print(cellPhoneNumbers[i][0] + " ");
   int phones = Integer.parseInt(cellPhoneNumbers[i][1]);
   for (int p = 0; p< phones; p++) {
        System.out.print("**");
   }
   System.out.println();
 }
(i=0;i{ 系统输出打印(手机号码[i][0]+“”); int phones=Integer.parseInt(手机号码[i][1]); 对于(int p=0;p

这是家庭作业吗使用电话作为索引是错误的,因为第二维度可以是0或1。。。在您的示例中,您使用手机的值作为索引:21

您可以使用以下代码:

 for (i = 0; i < ROWS; i++) {
   System.out.print(cellPhoneNumbers[i][0] + " ");
   int phones = Integer.parseInt(cellPhoneNumbers[i][1]);
   for (int p = 0; p< phones; p++) {
        System.out.print("**");
   }
   System.out.println();
 }
(i=0;i{ 系统输出打印(手机号码[i][0]+“”); int phones=Integer.parseInt(手机号码[i][1]); 对于(int p=0;p
这是吗