0

So I'm relearning java and it's been a while since. I'm trying to build a basic program (explained in code comments) and I'm having trouble remembering how to take user input and add it into an array. I'm having more trouble remembering how to loop through the user inputs and testing if they input anything as well as appending the input to the array if they do input something.

//This program will ask user for for there favorite four games
//If the answer is blank, it will ask again for a game title
//The program will than store there answers into an array
//The program will than display the array in random order
//it will then give the amount of games in the array with an integer



import java.util.*;

public class MultipleClassesMain {


public static void main(String[] args) {

    //Array holds 4 string inputs from user
    String gameArray[] = new String[4];

    //importing scanner element-------
    Scanner input = new Scanner(System.in);

    //Introduction---------------
    System.out.println("Hey there!!!");
    System.out.println("Please tell us four game titles you like to play!!!");

    //Asks what game user likes and takes user input into a variable
    System.out.println("So what a game you like?: ");
    String temp = input.nextLine();

    //This loop will test against blank user input
    while (temp.equals("") || (temp.equals("   ")){
        System.out.println("Your game can't be blank.  Enter again: ");

        }

    }

}

This is the code I have so far. If anyone could give me some constructive criticisms and some pointers on how to loop through user input(testing for input) and appending the inputs to the array, I'd greatly appreciate it.

Cheers

2 Answers 2

4

First: use a List instead of an array for user input. Just .add() your inputs to it. But see below for a better solution, ie using a Set.

Second: String has a .trim() method which removes whitespaces both at the beginning and end, use that and test for the empty string using .isEmpty().

Third: a List does not detect duplicate entries, however a Set does, provided that its entries correctly implement equals() and hashCode(), which String does, so the following code accounts for this (the .add() method of a Set returns true if and only if the set is modified as a result of the operation).

Sample code:

public static void main(final String... args)
{
    // Set of valid user inputs
    final Set<String> gameList = new HashSet<String>();
    // Object from which user inputs are read
    final Scanner in = new Scanner(System.in);

    // Introduction
    System.out.println("Hey there!!");
    System.out.println("Please tell us four game titles you like to play!!!");

    // What the user enters
    String input;

    // Check that 4 titles have been entered, don't get out of the loop until then
    while (gameList.size() < 4) {
        System.out.print("Enter the name of a game: ");
        // Read one input, trim all beginning and trailing whitespaces
        input = in.nextLine().trim();
        // If the resulting string is empty, input is invalid: ask for another
        if (input.isEmpty()) {
            System.out.println("Empty inputs not accepted!");
            continue;
        }
        if (!gameList.add(input))
            System.out.println("You have already selected this game (" + input + ')');
    }

    // Print out the list of inputs
    System.out.println("The list of selected games is: " + gameList);

}
Sign up to request clarification or add additional context in comments.

3 Comments

If you don't mind, could you give me an example of how this would be used?
See edit. There is one more thing to add to it however: duplicate entry detection. Added.
Thanks a ton. Thanks for commenting on the code and I better understand it now.
2
for (int i = 0; i < 4; i++) {
        String temp = input.nextLine();
        if (temp.equals("") || (temp.equals("   "))) {
            System.out.println("Your game can't be blank.  Enter again: ");
            i--;
        } else
            gameArray[i] = temp;

    }

Try this . This is what you asking for..yes?

1 Comment

I believe so. I forgot that I have to iterate through the list/array to make sure each spot is filled with an input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.