简体   繁体   English

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

[英]Convert string array to int array and then int to *

I am trying to take a string array made of countries and number of cell phones ex: peru 33. Then I want to convert the numbers into an integer and then replace the integer with ** to make a bar graph. 我正在尝试使用由国家和手机数量(例如秘鲁33)组成的字符串数组。然后,我想将数字转换为整数,然后将其替换为**以制作条形图。

Chile **************** 智利****************

Sweden ******* 瑞典*******

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

}

This prints out in a single line Chile 21 Sweden 11 etc. etc. I want them to print on separate lines. 这在一行中打印出了智利21瑞典11等。我希望它们在单独的行中打印。 I tried doing the print line int he second for loop with phones to print the integer 我尝试在第二行进行打印行以与手机循环以打印整数

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

but I get an error 但我得到一个错误

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

I'm getting very confused by this so any help would be appreciated. 我对此感到非常困惑,因此不胜感激。 thank you 谢谢

using phones as an index is an error, because the second dimension can be 0 or 1... In your examples you used the value of the phone as the index: 21 使用phones作为索引是错误的,因为第二维可以是0或1 ...在您的示例中,您使用电话的值作为索引:21

You can use this code: 您可以使用以下代码:

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

Is this homework? 这是作业吗? :D :D

I tried doing the print line int the second for loop with phones to print the integer 我尝试用电话在第二个for循环中执行打印行int以打印整数

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

Here you will have integer 21 after parsing cellPhoneNumbers[0][1] which is "21" and you are trying to access cellPhoneNumbers[i][phones] which is obviously outofbound because your array is String[][] cellPhoneNumbers = new String[5][2]; 在这里,您将在解析cellPhoneNumbers[0][1]后得到整数21 ,它是"21"并且您尝试访问的cellPhoneNumbers[i][phones]显然出站了,因为您的数组是String[][] cellPhoneNumbers = new String[5][2]; so for col>=2 it will be considered as outofbound for your array. 因此,对于col>=2 ,它将被视为阵列的出站。

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

If you want to print every element in new line you can try this. 如果要在换行中打印每个元素,可以尝试此操作。

   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 
     }

NOTE: You can also use System.out.print(cellPhoneNumbers[i][j] + "\\n"); 注意:您还可以使用System.out.print(cellPhoneNumbers[i][j] + "\\n"); if you want to. 如果你想。 :) :)

As the value of phones is the Integer value of cellPhoneNumbers[i][1] and the COL array is only 2 then it is bound to fail. 由于phones的值是cellPhoneNumbers[i][1]的整数值,并且COL数组只有2,因此必然会失败。

BTW, use System.out.println (...) to print on seperate line and why have the method return a String if all you will return is "" - have a void function instead. 顺便说一句,使用System.out.println(...)在单独的行上打印,如果您要返回的所有内容都是“”,为什么要让该方法返回String呢-而是使用void函数。

To print out the * try 打印*试试

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

Well, in the first iteration, you parse an int named phones from the array value in position [0][1] . 好吧,在第一次迭代中,您从位置[0][1]的数组值中解析了一个名为phonesint值。 This value is "21" , so the int you parse will have value 21. 此值为"21" ,因此您解析的int值为21。

That being the case, this line: 既然如此,这一行:

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

Is equivalent to doing: 等同于做:

System.out.print(cellPhoneNumbers[0][21]);

There is no value on that position since the array only has 5 rows and 2 columns, hence the ArrayIndexOutOfBoundsException . 由于数组只有5行2列,所以该位置没有值,因此ArrayIndexOutOfBoundsException

In regard to printing them out on separate lines, you can use System.out.println("...") instead of System.out.print("...") 关于将它们打印在单独的行上,可以使用System.out.println("...")代替System.out.print("...")

HAve edited your code.Try this one: 已编辑您的代码。尝试以下方法:

 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

        }

Your first question, to print in separate lines, you would need to do System.out.println(cellPhoneNumbers[i][j]) (notice the println instead of just print) 第一个问题,要以单独的行打印,您需要执行System.out.println(cellPhoneNumbers [i] [j])(注意println而不是仅打印)

To print out a number of stars that relates to the calling code, you need to loop over the number and print out each * instead of doing cellPhoneNumbers[i][phone] -- by using "phone" as the array index, you are referencing the 21st index which is obviously out of bounds. 要打印出与调用代码相关的星号,您需要遍历数字并打印出每个*而不是执行cellPhoneNumbers [i] [phone] -通过使用“ phone”作为数组索引,您可以引用显然超出范围的第21个索引。

So, to print out the stars, do the following inside your second for loop: 因此,要打印出星星,请在第二个for循环内执行以下操作:

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

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

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