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 8652663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:21:34+00:00 2026-06-12T14:21:34+00:00

I have a homework assignment which requires input from the user and stores it

  • 0

I have a homework assignment which requires input from the user and stores it in various data structures (arrays of linked-lists, stacks, etc.). However, I’ve been writing the main class of my previous homework assignments and this one in a very similar fashion. I have a very tiny main method. All the main method does is instantiate a new object which I don’t want to be destroyed and then loop through the program forever until the user chooses otherwise. I then have a menu() method which prints a list of selections and reads the user’s selection. And then from there, I pass that selection to another method which interprets the selection and performs accordingly.

The problem I have been having in the past assignments and now I’ve never really gotten a good answer for. The problem seems to lie with my menu() method and more specifically, the Scanner object. There always seems to be some junk left in the stream after I call the nextLine() method on a scanner object. So the next time the menu() method is called, it reads in that junk and loops though the rest of the program with that junk until menu() is called a third time. In the past, I would remedy this by calling the next() method right after I received my input and ignoring it. However, I seem to be having issues with that now as well.

In this program in particular, I have a method which request’s a user enter a city name. Now, city names can be more than one word (Palm City, West Palm Beach, Satellite Beach, New York, etc.). Now, when the scanner reads in one of those multi-word cities, it does the same thing as before, reads in some junk the next time the menu() method is called and goes though the whole program with it until menu() is called again. In this case, it prints an string “Invalid Selection” and then prints the menu again. I can’t for the life of me figure out what’s going on. any help would be appreciated.

import java.util.Scanner;


public class CSAir
{
    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args)
    {
        CityList flightLog = new CityList();
        boolean loop = true;

        while(loop)
        {
            loop = actions(menu(),flightLog);
        }

    }

    private static String menu()
    {
        System.out.print("Please Make a Selection\n" +
            "I) Insert a city\n" +
            "A)Add a flight path (One Way)\n" +
            "R) Request a flight\n" +
            "L) Load from a text file\n" +
            "Q) Quit\n" +
            "\nSelection: ");

        String in = input.next();
        //input.next();
        System.out.println();
        return in;
    }

    private static boolean actions(String selection, CityList flightLog)
    {
        if(selection.equalsIgnoreCase("I"))
        {
            insert(flightLog);
            return true;
        }

        else if(selection.equalsIgnoreCase("A"))
        {
            add(flightLog);
            return true;
        }

        else if(selection.equalsIgnoreCase("R"))
        {
            request(flightLog);
            return true;
        }

        else if(selection.equalsIgnoreCase("L"))
        {
            return true;
        }

        else if(selection.equalsIgnoreCase("Q")) return false;

        else 
        {
            System.out.println("Invalid Selection!\n");
            return true;
        }
    }

    private static void request(CityList flightLog)
    {
        System.out.print("Origin: ");
        String origin = input.nextLine();
        System.out.print("\nDestination: ");
        try
        {
            flightLog.isPath(origin, input.next());
        }
        catch (AllDestinationsVisitedException e)
        {
            e.getMessage();
        }
    }

    private static void add(CityList flightLog)
    {
        System.out.print("Origin: ");
        String origin = input.nextLine();
        System.out.print("\nDestination: ");
        flightLog.addPath(origin, input.next());
    }

    private static void insert(CityList flightLog)
    {
        System.out.print("Enter a City: ");

        try
        {
            flightLog.addCity(input.next());
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        System.out.println();
    }

}
  • 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-12T14:21:36+00:00Added an answer on June 12, 2026 at 2:21 pm

    Default Deliminator for scanner is white space. So when you enter New York since it has white space now scanner treats it as two tokens if you call next().

    A better option would be to use nextLine() method for reading such values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a homework assignment which asks to have the user input a date
I have a homework assignment where I need to take input from a file
Note: This is a homework assignment. I have two classes, one inherits from the
Have a homework assignment in which I'm supposed to create a vector of pointers
For a homework assignment I wrote some scala code in which I have the
A recent homework assignment I have received asks us to take expressions which could
I have a homework assignment to write a multi-threaded sudoku solver, which finds all
This was a homework assignment problem which I know I have incorrectly answered. I
I have a homework assignment in which I need to compute the edit distance
We've gotten a homework assignment in Java, which relates to inheritance. I don't have

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.