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

时间:2014-07-11 04:29:55

标签: java arrays

我正在尝试使用由国家和手机数量组成的字符串数组ex:peru 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 "";
        }

}

这打印出单行智利21瑞典11等。我希望它们在不同的行上打印。我尝试在第二个打印行中使用手机来打印整数

System.out.print(cellPhoneNumbers[i][phones])

但是我收到了错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 21
    at GraphNumbers.getArray(GraphNumbers.java:28)
    at CellPhoneGraph.main(CellPhoneGraph.java:8)
Java Result: 1 

我对此感到非常困惑,所以任何帮助都会受到赞赏。谢谢

6 个答案:

答案 0 :(得分:2)

使用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();
 }

这是家庭作业吗? :d

答案 1 :(得分:1)

  

我尝试在第二个for循环中使用手机来打印整数

int phones = Integer.parseInt(cellPhoneNumbers[i][1]);

在解析21 cellPhoneNumbers[0][1]之后,您将获得整数"21",并且您尝试访问cellPhoneNumbers[i][phones],因为您的数组是{{1}所以String[][] cellPhoneNumbers = new String[5][2];对于你的数组来说它将被视为outofbound。

col>=2

如果您想要在新行中打印每个元素,可以试试这个。

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

注意:如果您愿意,也可以使用 for (int i = 0; i < ROW ; i++){ for (int j = 0; j < COL; j++){ System.out.println(cellPhoneNumbers[i][j]); } //Use println instead of print } 。 :)

答案 2 :(得分:1)

由于phones的值是cellPhoneNumbers[i][1]的整数值且COL数组只有2,因此它必然会失败。

顺便说一下,使用System.out.println(...)在单独的行上打印,为什么方法返回一个String,如果你要返回的只是“” - 改为使用void函数。

打印出*试用

for (int x = 0; x < phones; x++) {
    System.out.print("*");
}
System.out.println("");

答案 3 :(得分:1)

好吧,在第一次迭代中,您从位置int中的数组值解析名为phones的{​​{1}}。此值为[0][1],因此您解析的"21"的值为21。

既然如此,这一行:

int

等同于:

System.out.print(cellPhoneNumbers[i][phones]);

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

关于在单独的行上打印它们,您可以使用ArrayIndexOutOfBoundsException代替System.out.println("...")

答案 4 :(得分:1)

编辑了你的代码。试试这个:

 for (i = 0; i < ROWS; i++)
        {

            for (j = 0; j < COL; j++)
            {
                if(j==0) //if it is first column value
                {
                System.out.print(cellPhoneNumbers[i][j]+" "); //this prints Country name and leaves a space as you need chillie (space) ****
                }
                if(j==1) //this checks that if value is second one as that is integer
                {
                    for(int x=0;x<Integer.parseInt(cellPhoneNumbers[i][j]);x++)  //if a int value so I have written a loop , which will print that many *
                    {

                        System.out.print("*");
                    }
                }

            }
            System.out.println(""); //So as if first row is done then it moves to next line to print

        }

答案 5 :(得分:0)

你的第一个问题,要分开打印,你需要做System.out.println(cellPhoneNumbers [i] [j])(注意println而不是打印)

要打印出与调用代码相关的一些星号,你需要循环显示数字并打印出每个*而不是使用“phone”作为数组索引来执行cellPhoneNumbers [i] [phone] ,你正在引用明显超出界限的第21个索引。

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

for (int k = 0; k < phones; k++) {
    System.out.print("*");
}
System.out.println(""); //this will provide a line break after the stars.
相关问题