[英]How do I display a string from an array from user input?
I have an array of 8 strings 我有8个字符串的数组
(Carter","Cocke","Washington","Greene","Hawkins","Johnson","Sullivan","Unicoi") (卡特”, “科克”, “华盛顿”, “格林”, “霍金斯”, “强生”, “苏利文”, “尤尼科伊”)
all referenced by "county" 全部由“县”引用
I'd prompt the user to input a number of 1-8 (iVal), 1 being Carter, 2 being Cocke, etc... 我提示用户输入1-8(iVal),1是Carter,2是Cocke,等等。
Is there a way I could do this other than using a switch statement? 除了使用switch语句之外,还有其他方法吗?
Also, how would I go about this if the user were to input Washington I'd display "Washington is in the array" and the opposite if the string isn't in the array? 另外,如果用户输入Washington我将显示“华盛顿在数组中”,而如果字符串不在数组中则显示相反的文字,我将如何处理呢?
Thanks in advance. 提前致谢。
Here is my array. 这是我的数组。
String [] county = {"Carter","Cocke","Washington","Greene","Hawkins","Johnson","Sullivan","Unicoi"};
for (int i = 0; i< county.length; i++)
{
System.out.print (county [i]+ "\n");
}
To prompt the user to enter a county, and then display it (without a switch) is simple enough. 提示用户输入一个县,然后显示它(不进行切换)非常简单。 You could use something like, 您可以使用类似
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);
}
As for testing if a String
array contains a particular String
you could write a utility function using the for-each
loop like 至于测试String
数组是否包含特定的String
您可以使用for-each
循环编写实用程序函数for-each
例如
public static boolean contains(String[] arr, String val) {
if (arr != null) {
for (String str : arr) {
if (str.equals(val)) {
return true;
}
}
}
return false;
}
i was working on the same thing, use ArrayList. 我在做同样的事情,使用ArrayList。 this was my code 这是我的代码
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);
}
}
now if u want a specific name from the list put this in system.out.println 现在,如果您要从列表中选择一个特定名称,请将其放入system.out.println
System.out.println("Name of the student: "+mylist. get(1) ); System.out.println(“学生姓名:” + mylist。get (1) );
now the trick is to let the user enter the number in get( ) 现在的诀窍是让用户在get()中输入数字
for this i made this program, here is the code 为此,我做了这个程序,这是代码
first make a scanner 首先做一个扫描仪
Scanner myScan = new Scanner(System.in);
int a = myScan.nextInt();
System.out.println("Name of the student: "+mylist.get(a));
now it will only print that name depending on what number user have entered !! 现在,它将仅根据用户输入的号码打印该名称!
I didn't understand well your questions, but I am going to answer on what I have understand. 我对您的问题不太了解,但是我将就我所了解的问题进行回答。
In case you want to print the value of a given index by the user here is the solution: Try an i
with correct (existing) index and another one which does not exist eg i=9
如果您要由用户打印给定索引的值,这里是解决方案:尝试使用具有正确(现有)索引的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");
}
}
}
In case you want to check if a given word exist you can do it that way: Again try a string which exist and one which does not 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.");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.