简体   繁体   中英

Can't figure out how to use getNewArrayFrom method with Scanner input?

I don't know if I'm being vague or not, but what my main goal is is to create a program in which it asks the user for a set of words (any given amount) and then tells me which is first and last in the alphabet and which is shortest and longest. So far I have this:

public static void main(String[] args)
{
    Scanner Keyboard = new Scanner(System.in);

    double[] Words = getNewArrayFrom(Keyboard);
    displayList(Words);

    int choice = 1;
    while(choice > 0)
    {
        System.out.println("What would you like to do?");
        System.out.println("1) Enter a new list of up to over 9000 words.");
        System.out.println("2) Find the shortest, longest, and first and last alphabetically from the list.");
        System.out.println("0) Exit the Program");

        choice = Integer.parseInt(Keyboard.nextLine());

        if(choice < 0 || choice > 2)
        {
            System.out.println("Invalid Choice Number " + choice);
            choice = 1;
            continue;
        }
        if(choice == 1)
        {
            //Enter the Words one at a time
            System.out.println("Enter each of the words.");
            for(int i = 0; i < 9001; i++)
            {
                System.out.print((i+1) + "> ");
                Words = Keyboard.nextLine();
                Words = getNewArrayFrom(Keyboard);
            }
        }
        else if(choice == 2)
        {

        }
    }
}

}

Sorry for the length but I have no idea what I'm doing. I've written this all from memory and I don't really know what to do.

My main point is just trying to allow the getNewArrayFrom() method to create an array from the input strings if chosen. If you can at least point me in the way or direction of how to get it to print the shortest and longest and first and last alphabetical words then I would be forever greatful!

Sorry for the English it is not my first language.

Since this is pseudo-homework, I'll leave out details.

I am going to assume that all the user input will be from one line, ie., they provide an input string such as apple banana orange grape . I am also going to assume each word is separated by spaces.

I would used the Scanner to read a single line of input, look into nextLine() . Then I would split the line that came back from the scanner by spaces, you can do this manually with a loop and indexOf(...) and substring(...) and store it in an array you created, or we can use the split() to have the built-in Java String functions handle that part for us.

If you use some_str.split(...) , it will give us a String[] array automaticaly, so you can return it if you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM