简体   繁体   中英

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.

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. 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

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

I tried doing the print line int the second for loop with phones to print the integer

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]; so for col>=2 it will be considered as outofbound for your array.

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"); 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.

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.

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] . This value is "21" , so the int you parse will have value 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 .

In regard to printing them out on separate lines, you can use System.out.println("...") instead of 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)

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.

So, to print out the stars, do the following inside your second for loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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