-4

In Java, I am creating a program that asks the user to think of someone they know.

Then, my program asks that they enter the first letter of their first name, and the last letter of their last name, with no spaces.

I want my program to then look through an array of whole names, find the one whose first letter matches the first letter of user input, with the corresponding last letter of their last name.

here is my program so far:

import java.util.* ;
public class Guesser
{
    public static void main(String[] args)
    {
        Scanner UserInput = new Scanner(System.in);
        String [] names = {"firstname lastname " + "etc"}; //example name array
        System.out.print( "Hello! I am a robot. I might be smart, but I don't know. Please play a game with me to help me see if I am smart." +  "\n"  + "What I want you to do is think of someone you know." +  "\n"  + "Enter the first letter of their first name, and the last letter of their last name. Please no spaces. Then, press enter. " );
        String TheirGuess = UserInput.nextLine(); //get their input, assign a string to it
        System.out.println("You entered: " + TheirGuess);
        char FirstChar = TheirGuess.charAt(0);  // get the the first char
        char SecondChar = TheirGuess.charAt(1);  // get the second char
        System.out.println("I will now think of someone whose first name starts    with " + FirstChar + " and last name ends with " + SecondChar );


        UserInput.close();


    }
}

How would I search in my string array for a name that has FirstChar as the first character and SecondChar as the last char?

Kara
  • 6,115
  • 16
  • 50
  • 57
Jeremy H
  • 95
  • 6

3 Answers3

2

This can be done in 1 line of code.

// Assuming you have populated a Set (actually any Collection) of names
Set<String> names;

List<String> matchedNames = names.stream()
    .filter(s -> s.matches(userInput.replaceAll("^.", "$0.*")))
    .collect(Collectors.toList());

If you just want to print the matches, it's even simpler:

names.stream()
    .filter(s -> s.matches(userInput.replaceAll("^.", "$0.*")))
    .forEach(System.out::println);

This code recognises that you can have multiple matches.

Although this may seem like spoon feeding, value to you of this answer is figuring out how it works.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

The efficient way to do this would be to use two TreeSet objects. One contains Names and the other contains Last names. Then you can use subSet() method to get entries. So, example:

TreeSet<String> names = new TreeSet<>();
names.add("Antonio");
names.add("Bernard");
names.add("Peter");
names.add("Zack");

Set<String> bNames = names.subSet("B", "C");

Note, that this implementation is case sensitive. But with few adjustments you can fix it - I'm leaving this to you.

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
0

Havn't written in Java for a while, but it should go something like this:

String names[] = new String[] { "AAA BBB", "CCC DDD", "EEE FFF" };
Scanner input = new Scanner(System.in);
String userInput = input.nextLine().toLowerCase();
String result = "None";
for (String name : names) {
    String[] nameSplitted = name.toLowerCase().split(" ");
    if (nameSplitted[0].charAt(0) == userInput.charAt(0) &&
        nameSplitted[1].charAt(0) == userInput.charAt(1)
    ) {
        result = name;
        break;
    }
}
System.out.println("Result is: " + result);
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36