我怎样才能创建一个java程序,它接受单词输入,找到某些单词,替换它们然后再打印出所有内容?

时间:2018-05-24 15:34:46

标签: java

我不知道该怎么做,我已经在网上找到了所有这些,我正在尝试改变它以做我上面解释的但是我被卡住了。基本上我想做的是将一篇文章复制并粘贴到代码中的某个地方,让它通过文章查看我告诉它要查找的任何单词,如果它找到它们,那么用我想要的单词替换它它来。

/**
     * 

    import java.util.Arrays;

    public class Main {
        public static String[] wordList(String line){
            return line.split(" ");
        }

        public static void main(String args[]) {    
            String words = "test words tesing";
            String[] arr = wordList(words);
            for(words i=0; i<words.length; i++)
            for (String s: arr)
                System.out.println(s);
        }

    }

    */

    import java.util.Scanner;
    import java.util.Arrays;

    public class Main {

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

1 个答案:

答案 0 :(得分:0)

要从键盘上阅读

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (isr);

String cadena = br.readLine();

你从键盘中引入的所有单词都将出现在&#34; cadena&#34;中。 要分离您介绍的所有单词,您可以使用从String.class拆分的方法。

String[] words = cadena.split(" ");

要查找特定单词,您可以使用方法,代码将在您的方法中:

String yourWord = "";
for(int i = 0; i < words.length; i++)
{
    if(words[i].equals("your word"))
    {
        yourWord = words[i];
        break;
    }
}

要补充一个单词,请使用方法replce(theWord,&#34;替换&#34;)

您可以使用循环打印所有单词,使用System.out.println(yourWord);