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

  • Home
  • SEARCH
  • 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 6881597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:07:41+00:00 2026-05-27T05:07:41+00:00

I’m making a text-based, menu driven program which uses the scanner class to take

  • 0

I’m making a text-based, menu driven program which uses the scanner class to take in both ints and strings. The ints correspond to the menu options, while the strings are used to take in user-input.

private static Scanner userInput = new Scanner(System.in);

public static void main(String[] args)
{
    //Will be used to initiate the while-loop
    int start = 1;

    while(start == 1)
    {
        System.out.print(Messages.printMenu());
        **int choice = new Integer(userInput.nextLine());**

        switch(choice)
        {
            case 1:

                System.out.println(Messages.askForAuthor());
                String author = userInput.nextLine();

                System.out.println(Messages.askForRecepName());
                String recepName = userInput.nextLine();

                System.out.println(Messages.askForEmailAdd());
                String recepEmail = userInput.nextLine(); 

                System.out.println(Messages.askForSubject());
                String subject = userInput.nextLine();

                System.out.println(Messages.askForTextBody());
                String textBody = "";
                ***while(!userInput.hasNext("end") && !userInput.hasNext("END"))***
                {
                    textBody +=  userInput.nextLine() + "\n";
                }

                System.out.println(author);
                System.out.println(recepName);
                System.out.println(recepEmail); 
                System.out.println(subject);
                System.out.println(textBody);
                break;

The parts surrounded by “**” is where the problem occurs. The program runs just fine during the first run, but when it goes into the while loop again for the second time, it causes a type mismatch error because “end”/”END” is still in the Scanner’s stack(I’m guessing it’s a stack) and choice looks for an int.

Here’s the output:

Document Storage System Menu
============================
1 - Create and store an e-mail
2 - Create and store a memo
3 - Create and store a report
4 - Display a document
5 - List all active documents
6 - List all archived documents
7 - Locate documents containing a specific word or phrase
8 - Archive a document
9 - Retrieve a document from the archive
10 - Clear the archive
99 - Quit

Enter your choice: 1
Please enter author: 
Agent Smith
Please enter the recipient's name: 
Neo
Please enter the recipient's e-mail address: 
Neo@zion.net
Please enter the subject: 
Notification for Eviction!
Please enter Enter text body (type END on separate line to stop): 
All your base are belong to us
end
Agent Smith
Neo
Neo@zion.net
Notification for Eviction!
All your base are belong to us

Document Storage System Menu
============================
1 - Create and store an e-mail
2 - Create and store a memo
3 - Create and store a report
4 - Display a document
5 - List all active documents
6 - List all archived documents
7 - Locate documents containing a specific word or phrase
8 - Archive a document
9 - Retrieve a document from the archive
10 - Clear the archive
99 - Quit

Enter your choice: Exception in thread "main" java.lang.NumberFormatException: For input string: "end"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.<init>(Integer.java:660)
    at proj4.Project4.main(Project4.java:20)***
  • 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-05-27T05:07:41+00:00Added an answer on May 27, 2026 at 5:07 am

    You should be able to do this:

    while(!userInput.hasNext("end") && !userInput.hasNext("END"))
    {
        textBody +=  userInput.nextLine() + "\n";
    }
    userInput.nextLine();
    

    Since at this point you know that either “end” or “END” is still waiting in the Scanner, you can just read the next line and do nothing with it. There are still issues about failing more gracefully if the wrong input is given, but this should solve the given problem.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
I am writing an app with both english and french support. The app requests

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.