【问题标题】:How to find if a string entered into scanner is in an array?如何查找输入扫描仪的字符串是否在数组中?
【发布时间】:2018-05-03 05:46:00
【问题描述】:

我必须:

提示用户输入字符串, 如果输入的字符串在数组“names”中,打印出它的索引, 如果不在名称中,则打印出“NOT FOUND”

这就是我所拥有的:

这是我的编程期末考试,我不知道该怎么做,因为我错过了 1 节课...

import java.util.Scanner;
public class Final1 {
   public static void main (String[] args) {
      int y;
      int x=0;

      Scanner s = new Scanner(System.in);

      String[] names = {"bob", "maxwell", "charley", "tomtomjack"};

      System.out.print("Enter String Name:");
         y=s.nextInt();

            for (String a: names){
               if (a.equals(y)) 
                  System.out.println(y);   
            }

   }
}

【问题讨论】:

    标签: java arrays string java.util.scanner


    【解决方案1】:

    使用Array.asList(yourArray).contains(yourValue):

    import java.util.*;
    
    public class Final1 
    {
    
    public static void main (String[] args) 
    {
         int y = 0;
         int x = 0;
         String name = "";
    
         Scanner s = new Scanner(System.in);
    
         String[] names = {"bob", "maxwell", "charley", "tomtomjack"};
    
         System.out.print("Enter String Name:");
    
         name = s.nextLine();
    
         if(Arrays.asList(names).contains(name)) // Check this line
         {
            System.out.print(name);   
         }
    
       }
    }
    

    【讨论】:

      【解决方案2】:

      如果您尝试再次打印它的索引而不是实际名称,则必须对其进行一些修改。

      import java.util.*;
      
      public class Final1 
      {
      
        public static void main (String[] args) 
        {
           int y = 0;
           int x = 0;
           String name = "";
           boolean found = false;
      
           Scanner s = new Scanner(System.in);
      
           String[] names = {"bob", "maxwell", "charley", "tomtomjack"};
      
           System.out.print("Enter String Name:");
      
           name = s.nextLine(); // get a string instead of an int
      
           // likely the way your professor would like you to do this
           // there are many ways, but this is the quickest while using a simple array
           // you could cast it to a list
           for(int i=0; i<names.length; ++i){
             if(names[i].equals(name)){
               System.out.print(i);
               found = true;
             }
           }
      
           if(!found)
             System.out.println("NOT FOUND");
        }
      }
      

      编辑:您也可以使用 Arrays 静态类。

      int result = Arrays.binarySearch(names, name);
      if(result > 0)
        System.out.println(result);
      else
        System.out.println("NOT FOUND");
      

      【讨论】:

        【解决方案3】:

        问题是你得到的用户输入是int

        用户输入值为String,以便检查名称,因为名称包含在String 数组中。

        你可以试试这个:

        import java.util.Scanner;
        public class MyClass {
            public static void main(String args[]) {
               String y;
              int x=0;
              int found = 0; //to identify the index 
        
              Scanner s = new Scanner(System.in);
        
              String[] names = {"bob", "maxwell", "charley", "tomtomjack"};
        
              System.out.print("Enter String Name:");
                 y=s.nextLine();
        
                    for (String a: names){
                       if (a.equals(y)) 
                        { 
                            System.out.println("index of "+a+" is :"+x);   
                            found++; // increment if found 
                        }
                        
                        x++;//iterate each time to get the index
                    }
                    
                    if(found == 0)// check if it is not found
                         System.out.println("NOT FOUND");
            }
        }
        

        输出:

        输入字符串名称:charley

        charley 的索引是:2

        【讨论】:

        • 谢谢 Minoli,我发现这很有帮助,现在我明白我做错了什么。
        • 现在你已经帮助了Help Vampire,这样做鼓励他们进一步在这个网站上转储他们的作业。
        • @CaptainMuffenz 欢迎。但要花一些时间,先尝试自己识别。然后您就可以轻松地学习和记住这些类型的错误。
        • @Hovercraft 同意。
        • @MinoliAlles 我确实尝试了 2 天。由于我错过了课程,无法弄清楚。它基本上拥有我需要知道如何做这个项目的一切
        【解决方案4】:

        您正在将int y 与扫描的整数进行比较。 y 应该是一个字符串,你应该扫描一个字符串。

        试试这个:

        import java.util.Scanner;
        public class Final1 {
           public static void main (String[] args) {
              int x=0;
        
              Scanner s = new Scanner(System.in);
        
              String[] names = {"bob", "maxwell", "charley", "tomtomjack"};
        
              System.out.print("Enter String Name:");
                 String y=s.nextLine();
        
                    for (String a: names){
                       if (a.equals(y)) 
                          System.out.println(y);   
                    }
        
           }
        }
        

        【讨论】:

        • 在我的课堂上,我们甚至从未讨论过如何将扫描仪变成字符串...... RIP
        • 现在你已经帮助了Help Vampire,并鼓励他们进一步在这个网站上转储他们的作业。
        【解决方案5】:

        您的y 应该是String,并且您应该使用s.nextLine() 输入它。目前您正在匹配整数和字符串。你可以使用indexOf

        int index = Arrays.asList(names).indexOf(s.nextLine());
        return index > -1 ? "Not found" : Integer.toString(index);
        

        【讨论】:

          猜你喜欢
          • 2011-08-23
          • 2016-06-20
          • 2015-05-27
          • 1970-01-01
          • 1970-01-01
          • 2020-09-07
          • 2012-12-26
          • 1970-01-01
          相关资源
          最近更新 更多