1

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");
      }
5
  • 1
    How does county[iVal-1] need a switch? Commented Nov 19, 2014 at 1:36
  • Why not just use System.out.println(county[i - 1]) where i is the value the user inputed (assuming you've done your range checking)? Commented Nov 19, 2014 at 1:36
  • @ElliottFrisch I'm not sure of your question, but iVal is the variable that holds the user input from 1-8. Commented Nov 19, 2014 at 1:43
  • Could I somehow use a 'for' statement somehow for both instances? Commented Nov 19, 2014 at 1:48
  • This smells like homework, which is a shame because you will not learn half as much about programming when you are fed the answers such as the ones below. Your actual question was "is a switch statement required?", to which the answer is no, it is not required. The first task does not even require a loop, while the second task can be written with a loop in about 6 lines of code. ..and you don't need to use an ArrayList. Commented Nov 19, 2014 at 1:54

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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 !!

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.