如何查找输入扫描仪的字符串是否在数组中?

时间:2018-05-03 05:46:01

标签: java arrays string java.util.scanner

我必须:

提示用户输入字符串, 如果输入的字符串在数组" names"中,则打印出其索引, 如果没有名字,请打印出来#34; NOT FOUND"

这就是我所拥有的:

这是我的编程决赛,我无法弄清楚如何做到这一点,因为我错过了一堂课...

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

   }
}

5 个答案:

答案 0 :(得分:3)

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

   }
}

答案 1 :(得分:3)

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

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

答案 2 :(得分:2)

您正在将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);   
            }

   }
}

答案 3 :(得分:2)

问题是您将用户输入设为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

     

查理指数是:2

答案 4 :(得分:1)

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

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