簡體   English   中英

Java:如何從數組中替換字符串元素

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

我是Java編程的新手,我正在嘗試創建一個程序,其中將要求用戶根據用戶指定的數字將單詞輸入數組。 之后,程序將按字母順序顯示輸入的單詞。 還將提示用戶“從列表中替換哪個單詞?” 這是我遇到問題的部分。 我不知道用戶如何輸入新單詞(字符串元素)並從列表中替換它。 我能想到的是輸入一個整數,表示該單詞在數組中的位置,並從列表中替換它。 不知道如何將其制成字符串?

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

    }
}

將您的代碼分解為較小的方法。 請參閱下面的(工作)示例。 您需要做的所有更改單詞(字符串,而不是數字索引)的方法是循環遍歷並使用equals方法檢查是否相等。 之后的break語句將停止迭代(找到正確的索引后,再也沒有任何意義了)。

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

樣本輸出:

您要輸入多少個單詞? 5
輸入單詞1:蘋果
輸入單詞2:香蕉
輸入單詞3:橙色
輸入單詞4:梨
輸入單詞5:檸檬
你的話是:
蘋果
香蕉
檸檬
橙子

您想更改哪個詞? 香蕉
您選擇了更改這個詞:香蕉
輸入新詞:草莓
你的話是:
蘋果
檸檬
橙子

草莓

替換之間的代碼

System.out.println(“您想更改哪個單詞?”);

System.out.println(“您已選擇更改單詞:” + sWordToChange);

有了這個:

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

說明:第一行只是為了避免掃描程序跳過對sWordToChange的分配。 for循環遍歷數組並查找匹配項,如果找到匹配項,則將單詞的索引保存在先前聲明的變量索引上。 如果找不到該單詞,則將其初始化為-1。 也許您想在替換之后立即添加一個if塊,以確保僅在匹配時才修改數組

在此程序中,需要添加sc.nextLine()才能在String數組中使用新單詞進行更新。Scanner類的nextLine()方法從緩沖區中獲取空字符串。

整個程序隨着代碼的改變。

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

Scanner類更改為BufferedReader類。

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

}}

您還可以在代碼中進行一些更改,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM