简体   繁体   中英

How do I display a string from an array from user input?

I have an array of 8 strings

(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...

Is there a way I could do this other than using a switch statement?

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?

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

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. 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("Name of the student: "+mylist. get(1) );

now the trick is to let the user enter the number in 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM