Q:

Java: How do I replace a string element from an array

Im a newbie in java programming and I'm trying to create a program wherein the user will be asked to input words into an array depending on the number specified by the user. Afterwards, the program will be displaying the entered words in Alphabetical order. The user will also be prompted "Which word to replace from the list?" This is the part I'm having problems. I dont know how the user can enter the new word (a string element) and replace it from the list. What I was able to come up, is to input an integer representing the position of that word from the array and replace it from the list. Dont know how can I make it as string?
import java.util.Scanner;
import java.util.Arrays;

public class EnterArrays {

    public static void main ( String args[] ){

        int length;
        Scanner sc = new Scanner(System.in);
        Scanner an = new Scanner(System.in);

        System.out.print("How many words are you going to enter? ");
        length = sc.nextInt();

        String[] sEnterWord = new String[length];

        for(int nCtr = 0; nCtr < length; nCtr++){
            System.out.print("Enter word " + (nCtr+1) + ":");
            sEnterWord[nCtr] = sc.next();
        }

        System.out.println("Your words are: ");
        Arrays.sort(sEnterWord);

        for(int nCtr = 0; nCtr < length; nCtr++){
            System.out.println(sEnterWord[nCtr]);
        }

        System.out.println("Which word would you like to change?");
        int sWordToChange = sc.nextInt();

        System.out.println("You have chosen to change the word : " + sWordToChange);

        System.out.println("Enter the new word: ");
        String sNewWord = an.nextLine();
        sEnterWord[sWordToChange-1] = sNewWord;

        System.out.println("Your words are: ");

        for(int nCtr = 0; nCtr < length; nCtr++){
            System.out.println(sEnterWord[nCtr]);
        }

    }
}

A:

Break your code up into smaller methods. See the (working) example below. All you need to do to change the word (string, not numeric index) with another is loop through and check for equality with the equals method. The break statement after that will stop iteration (after you've found the correct index, no point looking further). 
import java.util.Scanner;
import java.util.Arrays;

public class Test {

    public static void main ( String args[] ){
        String[] sEnterWord = getSortedWordArr();
        showWordlist(sEnterWord);
        String sWordToChange = getInputFromKeyboard("Which word would you like to change? ");
        System.out.println("You have chosen to change the word : " + sWordToChange);
        changeWordInArray(sWordToChange, sEnterWord);
        Arrays.sort(sEnterWord);
        showWordlist(sEnterWord);
    }

    private static String[] getSortedWordArr(){
        String line = getInputFromKeyboard("How many words are you going to enter? ");
        int length = Integer.valueOf(line);

        String[] sEnterWord = new String[length];

        for(int nCtr = 0; nCtr < length; nCtr++){
            sEnterWord[nCtr] = getInputFromKeyboard("Enter word " + (nCtr+1) + ":");
        }

        Arrays.sort(sEnterWord);

        return sEnterWord;
    }

    private static String getInputFromKeyboard(String prompt){
        System.out.print(prompt);
        Scanner s = new Scanner(System.in);
        String input = s.nextLine();

        return input;
    }

    private static void showWordlist(String[] words){
        System.out.println("Your words are: ");
        for (String w : words){
            System.out.println(w);
        }
    }

    private static void changeWordInArray(String word, String[] array){
        String newWord = getInputFromKeyboard("Enter the new word: ");

        for (int i = 0; i < array.length; i++){
            if (array[i].equals(word)){
                array[i] = newWord;
                break;
            }
        }

        Arrays.sort(array);
    }
}

Sample output: 
How many words are you going to enter? 5
Enter word 1:apple
Enter word 2:banana
Enter word 3:orange
Enter word 4:pear
Enter word 5:lemon
Your words are:
apple
banana
lemon
orange
pear
Which word would you like to change? banana
You have chosen to change the word : banana
Enter the new word: strawberry
Your words are:
apple
lemon
orange
pear
strawberry

