如何从用户输入中显示数组中的字符串?

时间:2014-11-19 01:33:42

标签: java

我有一个包含8个字符串的数组

(卡特”, “科克”, “华盛顿”, “格林”, “霍金斯”, “强生”, “沙利文”, “尤尼科伊”)

所有被“县”引用

我会提示用户输入1-8(iVal),1为Carter,2为Cocke等...

除了使用switch语句之外,有没有其他方法可以做到这一点?

另外,如果用户输入华盛顿我将如何显示“华盛顿在数组中”,如果字符串不在数组中则相反?

提前致谢。

这是我的阵列。

   String [] county = {"Carter","Cocke","Washington","Greene","Hawkins","Johnson","Sullivan","Unicoi"};
   for (int i = 0; i< county.length; i++)
      {
         System.out.print (county [i]+ "\n");
      }

3 个答案:

答案 0 :(得分:1)

提示用户进入县,然后显示它(没有开关)就足够了。你可以使用类似的东西,

String[] county = { "Carter", "Cocke", "Washington", "Greene",
        "Hawkins", "Johnson", "Sullivan", "Unicoi" };
Scanner scan = new Scanner(System.in);
for (int i = 0; i < county.length; i++) {
    System.out.printf("%d %s%n", i + 1, county[i]);
}
System.out.println("Please select a county from above: ");
int choice = scan.nextInt();
if (choice > 0 && choice <= county.length) {
    System.out.println("You selected: " + county[choice - 1]);
} else {
    System.out.println("Not a valid choice: " + choice);
}

至于测试String数组是否包含特定String,您可以使用for-each loop编写实用程序函数

public static boolean contains(String[] arr, String val) {
    if (arr != null) {
        for (String str : arr) {
            if (str.equals(val)) {
                return true;
            }
        }
    }
    return false;
}

答案 1 :(得分:0)

我正在做同样的事情,使用ArrayList。这是我的代码

import java.util.*;
 public class Practice{
    public static void main(String[] args){
       ArrayList<String> mylist = new ArrayList<>();
        mylist.add("Maisam Bokhari");
        mylist.add("Fawwad Ahmed");
        mylist.add("Ali Asim");
        mylist.add("Maheen Hanif");
        mylist.add("Rimsha Imtiaz");
        mylist.add("Mugheer Mughal");
        mylist.add("Maaz Hussain");
        mylist.add("Asad Shahzada");
        mylist.add("Junaid Khan");
        System.out.println("Name of the student: "+mylist);
      }
  }

现在,如果你想要列表中的特定名称,请将其放在system.out.println中         System.out.println(&#34;学生姓名:&#34; + mylist。 get(1));

现在的诀窍是让用户在 get()

中输入数字

为此我制作了这个程序,这里是代码

首先制作扫描仪

    Scanner myScan = new Scanner(System.in);
    int a = myScan.nextInt();


    System.out.println("Name of the student: "+mylist.get(a));

现在它只会打印该名称,具体取决于用户输入的号码!!

答案 2 :(得分:0)

我不太清楚你的问题,但我会回答我的理解。

如果您想要由用户打印给定索引的值,请执行以下解决方案: 尝试使用正确(现有)索引的i和另一个不存在的索引,例如i=9

public class app
{
   public static void main(String[] args)
   {
      String [] county ={"Carter","Cocke","Washington","Greene","Hawkins","Johnson","Sullivan","Unicoi"};
      int i = 10;  //i is the user input, you should do that using a BufferedReader or Scanner.
      try
      {
         System.out.println(county[i-1]);
      }
      catch(IndexOutOfBoundsException e)
      {
         System.out.println("This index doesn't exist");
      }
   }
}

如果您想检查给定单词是否存在,您可以这样做: 再次尝试一个存在的字符串和一个不存在的字符串。

public class app
{
   public static void main(String[] args)
   {
      String [] county = {"Carter","Cocke","Washington","Greene","Hawkins","Johnson","Sullivan","Unicoi"};
      String word = "Cocke";  //word is the user input, you should do that using a BufferedReader or Scanner.
      boolean found = false;
      for(int i=0; i<=7; ++i)
      {
         if(word == county[i])
         {
            found = true;
            break;
         }
      }

      if(found == true)
      {
         System.out.println(word + " is in the array.");
      }
      else
      {
         System.out.println(word + " is not in the array.");
      }
   }
}