简体   繁体   English

将二维数组中的字符串元素与 int 进行比较

[英]Comparing a string element in a 2d array to an int

I'm having trouble navigating 2d arrays. Let's say I'm using this string array:我在导航 2d arrays 时遇到问题。假设我正在使用这个字符串数组:

String[][] guestList = {{"Adam Ason", "35"},
                {"Berta Bson", "70"},
                {"Ceasar Cson", "12"}};

and I want to compare the age of each guest to 18 to determine if they are an adult or not, and print it, how would I go about it?而且我想比较每个客人的年龄和18岁来判断他们是不是成年人,然后打印出来,我怎么会go呢? So far I've tried:到目前为止,我已经尝试过:

int tempAge;
int adultGuests = 0;
int childGuests = 0;

for (int i = 0; i < guestList.length; i++)
{
     for (int j = 0; j < guestList.length; j++)
     {
         tempAge = Integer.parseInt(String.valueOf(guestList[j]));
         if (tempAge <= 18)
         {
             childGuests += 1;
         }
         else
         {
             adultGuests += 1;
         }
     }
}

System.out.println("Adult guests: " + adultGuests);
System.out.println("Children guests: " + childGuests);
  

Intellij tells me to wrap the ((guestList[j])) in String. Intellij 告诉我将 ((guestList[j])) 包装在字符串中。 valueOf but regardless if I do or not, I get the following result: valueOf但无论我是否这样做,我都会得到以下结果:

Exception in thread "main" java.lang.NumberFormatException: For input string: "[Ljava.lang.String;@5d22bbb7"
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.base/java.lang.Integer.parseInt(Integer.java:652)
        at java.base/java.lang.Integer.parseInt(Integer.java:770)
        at test2.main(test2.java:20)

You do not need inner loop.你不需要内循环。 You have fixed structure where the age is always at index 1 of your inner array.你有固定的结构,其中年龄总是在你的内部数组的索引1处。

So you need to iterate over the outer array and sum up elements of each inner array at the index 1 .因此,您需要遍历外部数组并在索引1处对每个内部数组的元素求和。

public static void main(String[] args) {
    String[][] guestList = {{"Adam Ason", "35"},
            {"Berta Bson", "70"},
            {"Ceasar Cson", "12"}};
    int adultGuests = 0;
    int childGuests = 0;

    for (int i = 0; i < guestList.length; i++)
    {
        if (Integer.valueOf(guestList[i][1]) <= 18){
            childGuests += 1;
        }
        else {
            adultGuests += 1;
        }

    }

You have a 2 dimensional array, ie an array of arrays, each inner array representing a guest.你有一个二维数组,即 arrays 的数组,每个内部数组代表一个客人。 You don't need a nested loop to calculate the count of adults and non adults but check for each inner array the integer value of the second element (at index 1):您不需要嵌套循环来计算成人和非成人的数量,而是检查每个内部数组第二个元素(索引 1 处)的 integer 值:

int tempAge;
int adultGuests = 0;
int childGuests = 0;

for (int i = 0; i < guestList.length; i++)
{
    tempAge = Integer.parseInt(String.valueOf(guestList[i][1]));
    if (tempAge <= 18)
    {
        childGuests += 1;
    }
    else
    {
        adultGuests += 1;
    }
}

System.out.println("Adult guests: " + adultGuests);
System.out.println("Children guests: " + childGuests);
String[][] guestList = {{"Adam Ason", "35"},
                {"Berta Bson", "70"},
                {"Ceasar Cson", "12"}};

This is your data.这是你的数据。 If you execute:如果你执行:

guestList[0];

The data that is returned is返回的数据是

{"Adam Ason", "35"}

and it is data like this you are trying to convert to an int, which is impossible.你正试图将这样的数据转换为 int,这是不可能的。

This is your current code, with one important remark.这是您当前的代码,其中有一个重要说明。

int tempAge;
int adultGuests = 0;
int childGuests = 0;

for (int i = 0; i < guestList.length; i++) {
     for (int j = 0; j < guestList.length; j++)  { // there is no need for this inner loop.
         tempAge = Integer.parseInt(String.valueOf(guestList[j]));
         if (tempAge <= 18) {
             childGuests += 1;
         } else {
             adultGuests += 1;
         }
     }
}

System.out.println("Adult guests: " + adultGuests);
System.out.println("Children guests: " + childGuests);

Simplifying your code like this, and using proper indices for a 2D array, should do the trick像这样简化您的代码,并为二维数组使用适当的索引,应该可以解决问题

int tempAge;
int adultGuests = 0;
int childGuests = 0;

for (int i = 0; i < guestList.length; i++) {
 tempAge = Integer.parseInt(String.valueOf(guestList[i][1])); // the age is always at index 1
         if (tempAge <= 18) { childGuests += 1; }
         else { adultGuests += 1; }
     
}

System.out.println("Adult guests: " + adultGuests);
System.out.println("Children guests: " + childGuests);

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

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