0

For the program below when I run it, it prompts the user to enter the number of strings they want to input into userStringInput. The problem that I'm having trouble fixing is when it entered into the for loop and asks the user to fill the items in the array it is automatically setting the first item of the array to an empty string and the first string that the user inputs is assigned to the [1] in the array and not [0]. Below I have included the code from the program as well as a sample of the code being run. Can you help me fix this array assignment error?

import java.util.Scanner; 
import java.util.Random; 

/**
Create a string array to store user input
Assign values to the String array
Create a random number generator
Store random numbers generated 
Create randomString Method to manipulate the initial array
Store manipulated Strings into a new array
Main method:
create an instance of the class
call get user input method 
store return array as an userDataArray in the main 
call randomString Method with userDataArray as an argument
store modifiedArray in main method
prints array
*/

public class StringManipulator {
    public String[] getUserData(){
        System.out.println("How many Strings will you enter?");
        Scanner keyboard = new Scanner(System.in);
        int totalNumberInArray = keyboard.nextInt();
        String[] userStringInput = new String[totalNumberInArray];
        for (int elementx = 0; elementx<userStringInput.length; ++elementx){
            System.out.println("Enter String Number " + (elementx+1) + ":");
            String userInput = keyboard.nextLine();
            userStringInput[elementx]= userInput; 
        }
        return userStringInput;
    }

    public void randomString(String[] tempStringArray) {
        Random generator = new Random();
        for (int elementy = 0; elementy<tempStringArray.length; ++elementy){
            int number = generator.nextInt(4); 
            String[] modifiedArray = new String[tempStringArray.length];     
            if(number == 0){
                //intact
                System.out.println(tempStringArray[elementy]+" (intact)");
            }else if(number == 1){
                //reversed
                StringBuffer s = new StringBuffer(tempStringArray[elementy]); 
                System.out.println(s.reverse() + " (reversed)");
            }else if(number == 2){
                //lowercase
                System.out.println(tempStringArray[elementy].toLowerCase() + " (lowercase)");
            }else if (number == 3){
                //uppercase
                System.out.println(tempStringArray[elementy].toUpperCase() + " (uppercase)");
            }
        }
    }


    public static void main(String[] args){
        StringManipulator instance = new StringManipulator();
        String[] tempStringArray = instance.getUserData();
        instance.randomString(tempStringArray);
    } 
}

Sample output:

How many Strings will you enter?
10
Enter String Number 1:
Enter String Number 2:
dog
Enter String Number 3:
cat
Enter String Number 4:
goose
Enter String Number 5:
moose
Enter String Number 6:
goose
Enter String Number 7:
cow
Enter String Number 8:
horse
Enter String Number 9:
goat
Enter String Number 10:
mule
 (uppercase)
DOG (uppercase)
tac (reversed)
goose (lowercase)
esoom (reversed)
esoog (reversed)
COW (uppercase)
esroh (reversed)
goat (intact)
MULE (uppercase)
azro
  • 35,213
  • 7
  • 25
  • 55

0 Answers0