0

I am trying to find an array in a two dimensional array using a string input from the user. But, I keep getting the "String[][] cannot be converted to to String" error. Can I do this with a keyboard scanner and strings or is there a more logical solution to this problem.

import java.util.Scanner;

public class QandA{

    public static void main(String[] args){
        String entry;
        String[] Why = new String[]{"Because.", "Just Because.", "Why yourself."};
        String[][] Questions = new String[][] { Why };
        Scanner k = new Scanner(System.in);
        entry = k.next();
        for (int i=0 ; i < Questions.length ; i++){
            if (entry.equalsIgnoreCase(Questions)){
                System.out.println("Test");
                break;
            }
            if (i == Questions.length){
                if (!entry.equalsIgnoreCase(Questions)){
                    System.out.println("Test2");
                }
            }
        }   
    }
}

EDIT:

I have changed my 2D array into a hashmap but am getting a "cannot find symbol, class Hashmap" even after importing java.util.HashMap; Help? [FIXED]

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.util.*;

public class QandA{

    public static void main(String[] args){
        String UE;
        String[] Why = new String[]{"Because.", "Just Because.", "Why yourself."};
        Map<String, String[]> Questions = new HashMap<>();
        Questions.put("Why", Why);
        Scanner k = new Scanner(System.in);
        UE = k.next();
        if(Questions.keySet().stream().filter(UE::equalsIgnoreCase).findFirst().isPresent()) {
            System.out.println("Test");
        } else {
            System.out.println("Test2");
        }   
    }
}
  • It's a bit unclear what you want to compare`entry` with. With any of the words in `Questions`? Just the first one of each question? Obviously, repeating `if (entry.equalsIgnoreCase(Questions)){` 10 times without changing anything makes no sense at all. `if (entry.equalsIgnoreCase(Questions[i][0])){` probably compiles, and might even make a bit of sense, depending on your requirements. – Erich Kitzmueller Mar 02 '17 at 16:42
  • @ammoQ I want it to check for the name of the array in Questions that is equal to entry. Then, print "Test" if it is found or "Test2" if it cannot be found. – Henry Fieglein Mar 02 '17 at 16:51
  • http://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable – Mawcel Mar 02 '17 at 17:07
  • @Mawcel that's very interesting, but this question is a blatant [XY Problem](http://meta.stackexchange.com/q/66377) – Patrick Parker Mar 02 '17 at 17:16
  • After reading up on XY problems i realized that you are correct next time I will attempt to ask a more open question to make room for a broader answer range. – Henry Fieglein Mar 02 '17 at 17:54
  • @HenryFieglein P.S. it's HashMap not Hashmap. Java class names are case sensitive. – Patrick Parker Mar 02 '17 at 18:05

1 Answers1

1

First you need to use a Map (instead of a String[][]) to map the name of the array variable to its instance:

String[] Why = new String[]{"Because.", "Just Because.", "Why yourself."};
Map<String, String[]> Questions = new HashMap<>();
Questions.put("Why", Why);

Next, you can perform the Test / Test2 check in many ways. Here is one way:

if(Questions.keySet().stream().filter(entry::equalsIgnoreCase).findAny().isPresent()) {
    System.out.println("Test");
} else {
    System.out.println("Test2");
}   

As a side note, your variable names are very misleading. "Entry" has a different meaning in the context of maps, since it encapsulates a Key+Value Pair. You should use meaningful variable names and conform to existing Java conventions regarding case, etc.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • Is there anything else to this because i am getting an error saying: cannot find symbol Map And cannot find symbol hashmap – Henry Fieglein Mar 02 '17 at 17:21
  • @HenryFieglein that's because you have to add the import statements to the top of the file. Just like you imported Scanner, you have to import Map and HashMap. – Patrick Parker Mar 02 '17 at 17:24
  • I suggest you study up on the documentation for Map and it's keySet, entrySet, put, and get methods. – Patrick Parker Mar 02 '17 at 17:25