【问题标题】:Storing user input in a string array将用户输入存储在字符串数组中
【发布时间】:2018-08-26 12:33:35
【问题描述】:

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

【问题讨论】:

  • 请将您的代码粘贴到问题中。
  • 在for循环外声明String []语句。

标签: java arrays string java.util.scanner


【解决方案1】:

您的解决方案中不需要字符串数组。

试试这个:-

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

【讨论】:

    【解决方案2】:

    我进行了以下更改以使您的代码正常工作。请注意,您错误地存储了输入字符串。在您的代码中,整个代码被存储为数组中的第一个索引。您不需要第一个 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);
             }
          }
    
       }
    }
    

    【讨论】:

      【解决方案3】:

      我认为你的范围有问题。 sentence[] 在你的第一个 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++;
          }
      
      }
      

      乌龟

      【讨论】:

        【解决方案4】:
        • 语句变量只定义在for循环内部,需要在for循环外部声明
        • 您可以再次使用第一个 Scanner (sc) 声明的变量,而不是声明另一个 (sc2)
        • sentence[i] = input -- 意思是 -- sentence[0] = "I love apple"
        • Scanner 变量可以代替 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);
            }
        }
        

        【讨论】:

        • 你为什么把equals改成matchesmatches 支持正则表达式,所以如果用户尝试匹配.a.a.a,那么它将匹配像banana 这样的词(因为在正则表达式中. 代表任何字符)。如果要比较文字,请使用 equals
        【解决方案5】:
        • 在 for 循环外声明 String [] sentence。它在第一个 for 块之外不可见。
        • 语句数组在for循环的迭代过程中一遍又一遍地创建。从命令行获取字符串不需要循环。

        • 编辑了我的答案并删除了任何 for 循环的使用,Arrays.asList() 将采用words 数组并从生成的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));
              }
          }
          

        【讨论】:

        • 这如何回答问题的第二部分?
        【解决方案6】:

        你只需要在for循环之外声明语句数组,目前问题是范围。For more on the scope of a variable in java 。另外,这不是您打算做的,您打算将输入作为命令行。

        因此,您将获得的输入将来自 String[] args。有关命令行参数的更多信息check here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-04-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-17
          相关资源
          最近更新 更多