-2

Below is my Java program. Right now, the only way for the program to run correctly is to have the last column be the longest. If the last column in the array isn't the longest, then the output isn't correct. For example, if the food array has more elements in the second column than in the third column like this:

String[][] food = {
        {"Bannana", "Apple", "Pear", "Orange"}, // fruits
        {"GreenBean", "Iceburg", "Spenach", "peas", "carrots", "potatoes", "beans"}, // vegetables 
        {"Steak",   "Baccon", "Beef", "TurkeyB", "TurkeyBacon", "Chicken"} // meats 
};

then if, for example, chicken is entered as input, the output is this:

Yo have no favorite food
Your favorite food is Chicken

And also, if the input, for example, is biscuit, then the output is this:

Yo have no favorite food
Yo have no favorite food

So, is there any way to only print out "Yo have no favorite food" once or correctly print out the favorite food without the last column having to be the longest?

Here is the code:

package com.begg;

import java.util.*;

public class List {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        String[][] food = {
                {"Bannana", "Apple", "Pear", "Orange"}, // fruits
                {"GreenBean", "Iceburg", "Spenach", "peas"}, // vegetables 
                {"Steak",   "Baccon", "Beef", "TurkeyB", "TurkeyBacon", "Chicken"} // meats 
        };

        for(int row = 0; row < food.length; row++) {
            for (int col = 0; col < food[row].length; col ++) {
                System.out.print(food[row][col] + ", ");
            }
            System.out.println();
        }
        System.out.println("Enter your favorite out of the options above:");
        String k = sc.next();
        loop2:
        for(int row2 = 0; row2<food.length; row2++) {
            for (int col2 = 0; col2< food[row2].length; col2++) {
                if (k.equalsIgnoreCase(food[row2][col2])) {
                    System.out.println("Your favorite food is " + food[row2][col2]);
                    break loop2;
                }
                else if (!(k.equalsIgnoreCase(food[row2][col2])) & col2== 5 ) {
                    System.out.println("Yo have no favorite food");
                }
            }
        }

    }
}

Thanks!

0xCursor
  • 2,230
  • 4
  • 14
  • 32
Nicholas Begg
  • 17
  • 1
  • 7

2 Answers2

0

Used Hashset instead of two for to iterate on 2d array. Code like ;

import java.util.HashSet;
import java.util.Scanner;

public class List {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        String[][] food = {
                {"Bannana", "Apple", "Pear", "Orange"}, // fruits
                {"GreenBean", "Iceburg", "Spenach", "peas"}, // vegetables
                {"Steak", "Baccon", "Beef", "TurkeyB", "TurkeyBacon", "Chicken"} // meats

                /*
                 * Your longest part of your array should be the longest
                 * if it's not, using the length  of the column to end a loop may cause problems
                 * if you use the row instead, everything after the row three will execute
                 *
                 */


        };

        for (int row = 0; row < food.length; row++) {
            for (int col = 0; col < food[row].length; col++) {
                System.out.print(food[row][col] + ", ");
            }
            System.out.println();
        }

        System.out.println("Enter your favorite out of the options above:");
        String k = sc.next();

        final HashSet<Object> setOfFood = new HashSet<>();
        //Add all food to hashset
        for (String[] subFoodList : food) {
            for (String subFood : subFoodList) {
                setOfFood.add(subFood.toLowerCase());
            }
        }

        //if set containts scanners string returns relevant response
        if (setOfFood.contains(k.toLowerCase())) {
            System.out.println("Your favorite food is " + k);
        } else {
            System.out.println("Yo have no favorite food");
        }

    }
}
drowny
  • 1,954
  • 8
  • 18
0

Changing this part:

else if (!(k.equalsIgnoreCase(food[row2][col2])) & col2== 5 ) {
    System.out.println("Yo have no favorite food");
}

to this:

else if (!(k.equalsIgnoreCase(food[row2][col2])) && row2 == food.length-1 && col2 == food[row2].length-1) {
    System.out.println("Yo have no favorite food");
}

should do what I think you are looking for. The edited else if checks that all the values of the array have been checked (doesn't matter which column is longer) and if none match the input, then it will execute the System.out.println("Yo have no favorite food"); once.

0xCursor
  • 2,230
  • 4
  • 14
  • 32
  • Thanks!!! And this is the part i'm trying to fix. I was planning to add more veggies, but why do you subtract 1 form food.length? – Nicholas Begg Sep 03 '18 at 17:26
  • I subtract 1 because the highest index of an array is length-1. For example, if an array has a length of 2, the element indexes are 0 and 1. If you don't subtract the 1, then you will an `ArrayIndexOutOfBoundsException`. – 0xCursor Sep 03 '18 at 17:27
  • [This post](https://stackoverflow.com/a/5554781/6214491) explains the `ArrayIndexOutOfBoundsException` well. – 0xCursor Sep 03 '18 at 17:40