Home > OS >  comparing a string element in a 2d array to an int
comparing a string element in a 2d array to an int

Time:10-07

I'm having trouble navigating 2d arrays. Let's say I'm using this string array:

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? 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.valueOf but regardless if I do or not, I get the following result:

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)

CodePudding user response:

You do not need inner loop. You have fixed structure where the age is always at index 1 of your inner array.

So you need to iterate over the outer array and sum up elements of each inner array at the index 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;
        }

    }

CodePudding user response:

You have a 2 dimensional array, i.e. an array of arrays, each inner array representing a guest. 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):

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

CodePudding user response:

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.

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);
  • Related