Java Program that finds the smallest word from an String array created from user input

Limbo

I have a program that asks the user for a certain number of inputs (words) and asks for said inputs before appending them to an array. It is then supposed to sort through them and find the word of the smallest length. However, by the time I enter what should be the second to the last input, the program breaks and displays a "0".

There are two methods being used.

Main:

public static void main(String[] args) {
  
  //Take user input to start program
  Scanner sc = new Scanner(System.in);
  System.out.println("Please enter the amount of words you'll input:");
  int inputInt = sc.nextInt(); //The size of the array

  //Set and initialize array
  String[] wordsArray = new String[inputInt];

  //Ask for input and append them to array
  System.out.println("Please input the words of your choice:");
  for (int i = 0; i < inputInt; i++) {
     wordsArray[i] = sc.nextLine();
  }//End of for loop

  //Call method
  System.out.println(minWordLength(wordsArray));

  sc.close();

}//End of main

The method called:

public static int minWordLength(String arr[]) {

  String smallestWord = arr[0];
  for (int i = 1 ; i < arr.length ; i++) {
     if (arr[i].length() < smallestWord.length()) {
        smallestWord = arr[i];
     }// End of conditional
  }// End of for loop

  return smallestWord.length();

}//End of minWordLength

What's wrong?

Gurkirat Singh Guliani

The problem comes after reading the input from your scan.nextInt() method of Scanner class, the cursor remains on the same line. so your enter is taken as input when you use nextLine() for first word in the array and that is why it gives 0 as output. . [https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo]

To resolve this, what you can do is that after taking your integer input, consume the left over line by using the nextLine(), then your code works fine as expected, Here is the rectified code :

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        // Take user input to start program
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the amount of words you'll input:");
        int inputInt = sc.nextInt(); // The size of the array
        sc.nextLine();
        // Set and initialize array
        String[] wordsArray = new String[inputInt];

        // Ask for input and append them to array
        System.out.println("Please input the words of your choice:");
        for (int i = 0; i < inputInt; i++) {
            wordsArray[i] = sc.nextLine();
        } // End of for loop

        // Call method
        System.out.println(minWordLength(wordsArray));

        sc.close();

    }

    public static int minWordLength(String arr[]) {

        String smallestWord = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i].length() < smallestWord.length()) {
                smallestWord = arr[i];
            } // End of conditional
        } // End of for loop

        return smallestWord.length();

    }// End of minWordLength2

}

which gives the output :

Please enter the amount of words you'll input:
2
Please input the words of your choice:
abde
as
2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Function to Find longest word in wordlist created from user input

get input from user and store it in String Array in Java

script that finds a word from an array and adds tooltip on it

User input to select from a string array

Finding the mode of an array from user input java

Comparing user input to values from an array Java

take input for array from user in java

Java: Value repeats in array from input of user

Program is not taking input from user

How to pass an input string from user to a MIPS program

Display the smallest number from an input in Java

Writing a program that, if given an array of characters, will output array that has the indices in order from largest to smallest (java)

How do I write a code that takes user input to form an array, and from that array the user decides if we should find the smallest or largest value?

Sort array from smallest to largest using java

Finding smallest value from array in java

Printing results from an array seperately from user input in java

How to get a multiple-line input from a user in Java and then store the input in an array of type string where every line is an element of the array

Accept 5 numbers from user input and output the largest and smallest

(java) - Storing each word from an input file in an array of Strings

Scala program that gives me smallest and largest number from an Array

Issues with structure arrays and obtaining array string from user integer input

Segmentation Fault for separating a string from user input than putting it to a array

Returning Unique Character Array (Unique String) from User Input

Changing a string from user input

Custom input to a Python program from a Java program

Array with size from user input

Find the most common word from user input

Why does the program not get input from the user?

This prolog program does not ask input from the user