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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:20:16+00:00 2026-06-18T02:20:16+00:00

here is the Menu class import java.util.Scanner; public class Menu { private String[] menu_options;

  • 0

here is the Menu class

import java.util.Scanner;

public class Menu {
private String[] menu_options;

public Menu(String[] menu_options) {
    this.menu_options = menu_options;
}

public int getUserInput() {
    int i = 1;
    for (String s : this.menu_options) {
        System.out.println(i + ". " + s);
        i++;
    }

    int selection = getint_input(menu_options.length);
    return (selection);
}

private int getint_input(int max) {
    boolean run = true;
    int selection = 0;

    while (run) {
        System.out.print("Select an option: ");
        Scanner in = new Scanner(System.in);


        if (in.hasNextInt()) {
            int value = in.nextInt();
            if(value>=1 || value<=max){
                selection = value; //fixed this now working
                run = false;    
            }

        } else {
            System.out
                    .print("Invalid input. Please enter a integer between 1 and "
                            + max + ": ");

        }
    }
    return selection;
}

}

and here is the menudriver i was using

public class Menutester {

public static void main(String[] args) {
    String[] menuitems = new String[2];
    menuitems[0] = "option one";
    menuitems[1] = "option two";
    Menu tm = new Menu(menuitems);
    int choice  = tm.getUserInput();
    System.out.println("Got input");
}

}

the first time i input something it dosn’t regester at all and when i try to debug it in eclipse it gives me the error FileNotFoundException(Throwable).(String) line: 195 on the first input.

this is what it returns

  1. option one
  2. option two
    Select an option: 1(i entered this and pressed enter)
    1(same here only it regestered the input)
    Got input
  • 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-18T02:20:17+00:00Added an answer on June 18, 2026 at 2:20 am

    nextInt reads the input and removes it from the buffer. You can’t call it like that without storing the value.

    Call it once, store the value, and then do all the checks needed.

    Change this:

    if (in.hasNextInt() && in.nextInt() >= 1 || in.nextInt() <= max) {
                selection = in.nextInt();
    //...
    

    for this:

    if(in.hasNextInt()) {
       int selection = in.nextInt();
       if(selection >= 1 || selection <= max) {
           run = false;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this code : package Firstpack; import java.io.*; import java.util.*; public class
I have a problem with my project here is my LoginActivity import java.util.ArrayList; import
Here is my code for tabbed screen: CareActivity.xml import java.util.HashMap; import android.content.Context; import android.os.Bundle;
here is my Code package com.example.messenger; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress;
I have been having trouble while attempting to use the nextLine() method from java.util.Scanner.
I'm trying to change the menu here from mouse over to a on click
I have built a drop-down menu here http://dev.driz.co.uk/jsonmenu/ that uses JSON to dynamically build
I look at example of dropdown menu here . Here is simplified jsfiddle example.
I am useing the superfish jquery plugin If you view the superfish menu here:
I have a menu written here which contains root elements and secondary elements. I

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.