0

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]);
        }

    }
}
4
  • you have to check each words in the list with the given string of the user. And on match, you replace the string with the new input and print out the new list. - See String.equals(String) method. Commented Jan 24, 2016 at 12:35
  • you want to change a word, yet you ask for "nextInt()". Commented Jan 24, 2016 at 12:43
  • so do I use a for loop again to check for the match, then inside the loop, use an if statement implementing the String.equals(String) method? Commented Jan 24, 2016 at 12:47
  • Yes I tried changing it to string, but I keep on getting an error about incompatible data types Commented Jan 24, 2016 at 12:49

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your help! I was able to fix my program :)
0

Replace the code between

System.out.println("Which word would you like to change?");

and

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

with this:

sc.nextLine();
String sWordToChange = sc.nextLine();
    int index=-1;
    for (int i = 0; i < sEnterWord.length; i++) {
       if(sEnterWord[i].equals(sWordToChange))
           index=i;
    }

Explanation: The first line is just to avoid the scanner skipping the assignation to sWordToChange. The for loop iterates over the array and looks for a match, if it finds one it saves the index of the word on the previously declared variable index. It is initalized as -1 in case the word is not found. Maybe you want to add an if block right after the substitution to make sure you modify the array only when there is a match

Comments

0

Changed the Scanner class to BufferedReader Class.

public class ArrayTest {

public static void main(String args[]) throws IOException {

    int length;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.print("How many words are you going to enter? ");
    length=Integer.parseInt(br.readLine());

    String[] sEnterWord = new String[length];

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

    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?");

    String sWordToChange = br.readLine();


    System.out.print("Enter the new word: ");
    String sNewWord = br.readLine();

    for (int i = 0; i < sEnterWord.length; i++) {
        if (sEnterWord[i].equals(sWordToChange)) {
            sEnterWord[i] = sNewWord;

        }
    }

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

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

}}

you can also make some changes in the code like:

System.out.println("Which word would you like to change?");

    String sWordToChange = br.readLine();

    int position = Arrays.binarySearch(sEnterWord, sWordToChange);
    if (position < 0) {
        System.out.println("Word not found ");

    } else {
        System.out.print("Enter the new word: ");
        String sNewWord = br.readLine();
        sEnterWord[position] = sNewWord;
    }

Comments

0

In this program you need add sc.nextLine() before taking new word to update in String array.nextLine() method of Scanner class takes empty string from buffer.

The whole program with change of code.

package expertwebindia;
import java.util.Arrays;
import java.util.Scanner;
public class Test {
    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();
        sc.nextLine();
        System.out.println("You have chosen to change the word : " + sWordToChange);

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

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

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

1 Comment

hmm not sure seems like its still asking for integer when I tried it :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.