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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:24:12+00:00 2026-06-11T21:24:12+00:00

This is a small part of my program that I am working on. I’m

  • 0

This is a small part of my program that I am working on. I’m trying to check if the user enters the correct number.

They have five choices to choose from so they can either hit 1, 2, 3, 4, or 5. Then press enter.

So I want to check to make sure the user doesn’t type anything in < 1 or > 5. I got that part to work… But I just want to know if there is a easier way to do it then from what I did in code below.

The next part is that I also want to make sure the user doesn’t type in letters. like “gfgfadggdagdsg” for a choice.

Here is my code of the part I am working on….

public void businessAccount()
    {


        int selection;

        System.out.println("\nATM main menu:");
        System.out.println("1 - View account balance");
        System.out.println("2 - Withdraw funds");
        System.out.println("3 - Add funds");
        System.out.println("4 - Back to Account Menu");
        System.out.println("5 - Terminate transaction");
        System.out.print("Choice: ");
        selection = input.nextInt();

            if (selection > 5){

            System.out.println("Invalid choice.");
            businessAccount();

        }
            else if (selection < 1){
                System.out.println("Invalid choice.");
                businessAccount();
            }
            else {

        switch(selection)
        {
        case 1:
            viewAccountInfo3();
            break;
        case 2:
            withdraw3();
            break;
        case 3:
            addFunds3();
            break;
        case 4:
            AccountMain.selectAccount();
            break;
        case 5:
            System.out.println("Thank you for using this ATM!!! goodbye");
        }
            }
    }
  • 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-11T21:24:13+00:00Added an answer on June 11, 2026 at 9:24 pm

    You may get rid of checking < 1 and > 5 by adding a default case.

    try{
         selection = input.nextInt();        
         switch(selection){
          case 1:
              viewAccountInfo3();
              break;
          case 2:
              withdraw3();
              break;
          case 3:
              addFunds3();
              break;
          case 4:
              AccountMain.selectAccount();
              break;
          case 5:
              System.out.println("Thank you for using this ATM!!! goodbye");
              break;
          default:             
              System.out.println("Invalid choice.");
              businessAccount();
    
          }
    }catch(InputMismatchException e){
        //do whatever you wanted to do in case input is not an int
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is just a small part of a bigger program but Im trying to
I have this small class called City that simply holds some information about a
I'm trying to write a program that cleans data, using Matlab. This program takes
I have this small function that is causing me headaches on RHEL6, and I
Hi I have a small problem. I am coding a program that displays the
I made this small Java program using eclipse IDE. I have set the workspace
Let's say you have a small calculator program that takes numbers and an operator
I am trying to understand some code. It is a small program that prints
On a website that I'm working on for my school, the user enters their
I am writing a small program as part of a University course. In this

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.