簡體   English   中英

將用戶輸入存儲在字符串數組中

[英]Storing user input in a string array

我是Java的初學者。 我嘗試編寫一個程序以從命令行參數中讀取一系列單詞,並找到給定單詞的第一個匹配項的索引。 像用戶可以輸入“我愛蘋果”,給定的單詞是“蘋果”。 程序將顯示“'apple'的第一個匹配項的索引為2”。

到目前為止,我所做的沒有用。 我將輸入存儲到字符串數組的方式不正確嗎?

import java.util.Scanner;

public class test {
    public static void main(String [] args) {

        System.out.println("Enter sentence: ");

        Scanner sc = new Scanner(System.in);

        String input = sc.nextLine();

        int num=1;
        String sentence[]=new String[num];

        for(int i=0; i< num; i++) {

          sentence[i] = input; // store the user input into the array.
          num = num+1;
        }


        System.out.println("Enter the given words to find the index of its first match: ");
        Scanner sc2 = new Scanner(System.in);
        String key = sc2.next(); 

        for(int j=0; j<num; j++) {
            while (sentence[j].equals(key)) {
                System.out.println("The index of the first match of "+key+" is "+j);
            }
        }
    }   
}

解決方案中不需要字符串數組。

嘗試這個 :-

    System.out.println("enter sentence ");
    Scanner sc = new Scanner(System.in);

    String input = sc.nextLine();

    System.out.println("enter the given word to fin the index ");

    sc = new Scanner(System.in);

    String toBeMatched = sc.nextLine();

    if (input.contains(toBeMatched)) {
        System.out.println("index is  " + input.indexOf(toBeMatched));
    } else {
        System.out.println("doesn't contain string");
    }
  • 句子變量僅在for循環內定義,需要在其外部聲明
  • 您可以再次使用第一個Scanner(sc)聲明的變量,而不用聲明另一個(sc2)
  • 句子[i] =輸入-表示-句子[0] =“我愛蘋果”
  • 掃描程序變量可以為您完成輸入數組而不是for循環的所有工作

String[] a = sc. nextLine(). split(" ");

這將掃描新行的輸入,並將每個由空格分隔的字符串分隔到每個數組中。

System.out.println("Enter sentence: ");

Scanner sc = new Scanner(System.in);

String[] sentence = sc. nextLine(). split(" ");

System.out.println("Enter the given words to find the index of its first match: ");
String key = sc.nextLine(); 

for(int j=0; j<num; j++) {
    if (sentence[j].matches(key)) {
        System.out.println("The index of the first match of "+ key +" is "+ j);
    }
}
  • 在for循環外聲明String [] sentence 在第一個for塊之外不可見。
  • 在for循環的迭代過程中,一遍又一遍地創建句子數組。 從命令行獲取String不需要循環。

  • 編輯了我的答案,並刪除了任何for循環, Arrays.asList()將使用words array並從結果List獲取單詞的索引。

     public static void main(String[] args) throws IOException { System.out.println("Enter sentence: "); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); System.out.println("Enter the given word to find the index of its first match: "); Scanner wordInput = new Scanner(System.in); String key = wordInput.next(); String[] words = input.split(" "); int occurence = Arrays.asList(words).indexOf(key); if(occurence != -1){ System.out.println(String.format("Index of first occurence of the word is %d", occurence)); } } 

您只需要在for循環外聲明句子數組,就目前而言,這個問題是有問題的。 有關java中變量范圍的更多信息 另外,這不是您打算要做的,您打算將輸入作為命令行。

因此,您將獲得的輸入將以String []參數形式出現。 有關命令行參數的更多信息, 參見此處

我認為您有范圍問題。 在您的第一個forloop中聲明並實例化了句子[]。 嘗試將聲明移出循環,您應該消除該錯誤。

我還注意到了一些錯誤。 你可以試試這個

public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Enter Sentence");
    Scanner sc = new Scanner(System.in);

    String input = sc.nextLine();
    String sentence[] = input.split("\\s");


    System.out.println("Enter Word");
    Scanner sc2 = new Scanner(System.in);
    String key = sc2.next();

    int index = 0;
    for(String word : sentence)
    {

        if(word.equals(key))
        {
            System.out.println("The index of the first match of " + key + " is " + index);
            break;
        }
        index++;
    }

}

我進行了以下更改以使您的代碼正常工作。 請注意您輸入的字符串存儲不正確。 在您的代碼中,整個代碼被存儲為數組中的第一個索引。 您不需要第一個for-loop因為我們可以使用函數.split()將每個單詞區分為數組中的不同索引。 其余代碼保持原樣。

public class ConfigTest {

   public static void main(String[] args) {

      System.out.println("Enter sentence: ");
      Scanner sc = new Scanner(System.in);

      String input = sc.nextLine();
      // Use this to split the input into different indexes of the array
      String[] sentence = input.split(" ");

      System.out.println("Enter the given words to find the index of its first match: ");
      Scanner sc2 = new Scanner(System.in);
      String key = sc2.next();

      for (int i = 0; i < sentence.length; i++) {
         if (sentence[i].equals(key)) {
            System.out.println("The index of the first match of " + key + " is " + i);
         }
      }

   }
}

暫無
暫無

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

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