简体   繁体   中英

How to Deal with Skipping Input for 1-Dimensional Array in Java

In this program, this user will have the opportunity to generate their own word search. In the beginning of the program, the user will be shown a menu of instructions in which they can choose between these options: 1. Create a word search 2. Print the word search 3. View solutions to the word search 4. Exit the program

When choosing to create a word search, the user will be asked to enter words of their choice, line by line. These words will be stored in a 1-D array. The user will have to enter a minimum of 20 words, maximum at 260. At every batch of 20 words, the user will be asked if they want to add more words. If they don`t, the program will jump right into converting the 1-D array to an Array List, and then create the word search. If the user chooses to add more words, the program will prompt him/her to enter more words until they have reached the max number of words. Options 2 and 3 will just involve some loops and using a few methods to display an organized output to the user.

The program is not letting me input words into the words array. When running the program, user enters "1" to create word search, then the program instructs user to input words line by line, but it is not letting the user input anything. The console screen reads "Word Search created" and right under this, it says "Invalid input, try again." I created the array list right after introducing the program: List<String> words = new ArrayList<>();

I tried to figure out where I was going wrong here, and I even tried searching up about this, but nothing really solved my problem.

 do { WordArray wordArr = new WordArray(); showOptions(); choice = input.nextInt(); // Get choice input if (choice == 1) { System.out.println("Enter words of your choice line-by-line. You can enter a maximum of 260 words (ie, 10 words per letter)"); System.out.println(""); // This for loop will loop around with it`s body the user decides they have added enough words and wish to proceed for (int i = 0; i < words.size(); i++) { words.add(input.nextLine()); if ((i + 1) % 20 == 0 && i != 0) { // For every batch of 20 words entered, the program will ask the user this... System.out.print("Do you want to keep adding words? Enter Y/N: "); String answer = input.next().toUpperCase(); if (answer.equals("Y")) { words.add(input.nextLine()); } if (answer.equals("N")) { break; }//end of inner if }//end of outer if }//end of for loop createWordSearch(words); 

from the discussion on this chat , the error was in the for-loop

for (int i = 0; i < words.size(); i++)

were words.size() was 0, so to fix that you should use

for (int i = 0; i <= 260; i++)

changing words.size() to 260, where 260 is the max amount of words that the user can enter.

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