Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9031883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:50:30+00:00 2026-06-16T07:50:30+00:00

So I’m relearning java and it’s been a while since. I’m trying to build

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:50:31+00:00Added an answer on June 16, 2026 at 7:50 am

    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);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.