I'm taking a beginning java class and I'm trying to create a helper method to get a input from a user and combine all four methods into a package I can use in my programs after. I keep running into trouble with the last method, getMenuStringFromUser, the error it gives me says java cannot find symbol method getIntegerFromUser(java.lang.String,int,int) here's what i have so far

package bulletproof;
import java.util.*;

//BULLETPROOF METHOD FOR INTEGERS
// better general method with low and high limits
class getIntegerFromUser {
    public static int getIntegerFromUser(String prompt, int low, int high) {
        Scanner keyboard = new Scanner(System.in);
        while (true) { // keep asking until we get an integer within limits low and high
            System.out.print(prompt);
            try {
                String input = keyboard.nextLine(); // read next line
                int theInteger = Integer.parseInt(input);
                if (low <= theInteger && theInteger <= high)
                    return theInteger;
            }
            catch (NumberFormatException e1) {}
            catch (NoSuchElementException e2) {}
            System.out.print("\nPlease enter a number within limits [" + low + "-" + high + "].\n");
            keyboard.close();
            keyboard = new Scanner(System.in);
        }
    }
}

//BULLETPROOF METHOD FOR DOUBLE
class getDoubleFromUser {
    public static double getDoubleFromUser(String prompt, double low, double high) {
        Scanner keyboard = new Scanner(System.in);
        while (true) { // keep asking until we get an integer within limits low and high
            System.out.print(prompt);
            try {
                String input = keyboard.nextLine(); // read next line
                double theDouble = Double.parseDouble(input);
                if (low <= theDouble && theDouble <= high)
                    return theDouble;
            }
            catch (NumberFormatException e1) {}
            catch (NoSuchElementException e2) {}
            System.out.print("\nPlease enter a number within limits [" + low + "-" + high + "].\n");
            keyboard.close();
            keyboard = new Scanner(System.in);
        }
    }
}


//BULLETPROOF METHOD FOR STRINGS
class getStringFromUser {
    public static String getStringFromUser(String prompt) {
        Scanner keyboard = new Scanner(System.in);
        while (true) { // keep asking until we get valid input
            try {
                System.out.print(prompt);
                String input = keyboard.nextLine().trim(); // trim the whitespace (leading and trailing white space)
                if (input.length() > 0) // then make sure there's at least one non-white space character
                    return input;
            }
            catch (NoSuchElementException nsee) {}
            System.out.println("\nPlease enter text (ex. yes)\n");
            keyboard.close();
            keyboard = new Scanner(System.in);
        }
    }
}

class getMenuStringFromUser {
    public static String getMenuStringFromUser(String title, String...items) {
        Scanner keyboard = new Scanner(System.in);

        //First build the menu title and items, putting a number before each item
        String menu = title + "\n\n";
        int count=0;
        while (count < items.length) {
            menu += "" + (count+1) + "." +items[count] + "\n";
            count++;
        }
        menu += "\nPlease enter your selection [1-" + items.length + "]: ";

        // Now call getIntegerFromUser to display the menu to get the user's numeric response
        int num = getIntegerFromUser(menu, 1, items.length);
        //Finally, return the String the user selected
        return items [num-1];
    }
}

Edited 4 Years Ago by asiawilliams

Misread your post at first. You are calling a method in a totally different class. Replace line 81:

getIntegerFromUser.getIntegerFromUser(menu, 1, items.length);

You have to reference the class and then call the static method.
Also, proper Java naming convention is to name classes with a capital letter.
You can consolidate these methods into 1 class if you wanted.

Edited 4 Years Ago by ianwalter: Misread problem

I changed the line, but I'm trying to conceptually understand why I had the wrong thing. I don't think i understand what you're saying neccessarily. My professor isn't the best explaining the java concepts and they're confusing once you get to methods..

Hmm... How about this...

I am guessing you implement all your classes in the same file? Even though they all are in the same file, each of them has its own space. In other words, each class has its own visibility and they sees only whatever implementation inside themself.

There are two ways to call a method of another class. One is to instantiate an object of the class, and then call the method via the class instance. The other, which is what you should do, is to implement static methods. Those methods can be called outside its class without the need to instantiate a class instance. The downside of the latter is that it would become somewhat immutable (the method does not allow modifying any variables if those variables are not static).

However, I see some issues in your code.

1)Why do you close the Scanner and reinstantiate over and over again if the parsing is unsuccessful?
2)If you close it after the try-catch, why don't you close it inside the try-statement but return the value without closing the Scanner?
3)Why do you rely on catch exception instead of handle an unexpected input instead?

Knowing that you are new to the language, it is nice to see that you try to implement something for yourself in the future. However, I am not so sure if it is a good thing to not commenting anything at all.

This article has been dead for over six months. Start a new discussion instead.