0

Ok so my question is , is there a way to loop something until a choice of Strings are inserted ?

        case "John":
            n = 12;
            break;

        case "Jenny":
            n = 6;
            break;
        default:
              System.out.print("Wrong Name");

Lets say in this i want to loop the user to input the name until he uses any of the above case values.Now i know that i can write a while loop and use the OR operator for each , But i have a lot of valid inputs so is there a simpler way to loop until correct switch name is entered by a user.? if its an incorrect one i want to display wrong name and prompt again for user to input

I am using Java.Any help regarding this is greatly appreciated. Thanks a lot in advance.

     public static int[] amount(int n) {
    int[] values = new int[6];  
    int i;


     i=n+6+6;
      values[a]=i;
     return values;
 }

 public static void returnarray(){

  int values[]=amount()
            int i = 0;
  if
   (values[i]%==0)
  {system.out.println("the value is an even value");

  else{ System.out.print("Not so even");
         }
    }

the issue i am having is when i am tryin to return the value from the first array the amount() method requires the parameter , i am not sure to return the first array to the second due to it having a parameter (int n) , im not sure if i am making enough sense to you and the code is not exactly how i typed it .

Il make it clearer . I need to return the n value from the switch into one method where i will be using that n method to do a certain caluculation multiple times and store those values inside an array. and i will return this array into another method where i will do another calculation there and display the out put; what i am having the issue with is on how to return the array into the second method becz the first method has a parameter which is (int n) as described by @kaushal28

  • To loop, you will need a loop :) but the trick is breaking the loop while in the `switch`. That is answered here: https://stackoverflow.com/questions/22823395/java-how-can-i-break-a-while-loop-under-a-switch-statement – Gabriel Luci Mar 14 '18 at 15:35

3 Answers3

1

I would suggest a Map<String, Integer> instead of a loop or a switch or if. It has the advantage of being O(1) and clean to implement. Like,

Map<String, Integer> map = new HashMap<>();
map.put("John", 12);
map.put("Jenny", 6);
String key = ""; // <-- your name field.
if (map.containsKey(key)) {
    System.out.println(map.get(key)); // <-- 12 or 6
} else {
    System.out.println("Wrong name");
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • for limited cases I think `Map` would be overhead, – Kaushal28 Mar 14 '18 at 15:42
  • If each `case` really does just that, then this is IMHO much clearer and less repeterive. And it will be less overhead (in terms of LOC) using Java 9's ridiculously overloaded [`Map.of`](https://docs.oracle.com/javase/9/docs/api/java/util/Map.html#of-K-V-K-V-). – tobias_k Mar 14 '18 at 15:45
  • (IMHO the example would be better replacing the `System.out` with `n = map.get(key); break;`) – tobias_k Mar 14 '18 at 15:48
1

You can use infinite loop for that and set a flag when one of the cases is encountered. For example:

boolean flag = false;
while(!flag){
    //take user input here.
    case "John":
        n = 12;
        flag = true; 
        break;

    case "Jenny":
        n = 6;
        flag = true; 
        break;
    default:
          System.out.print("Wrong Name");
}

EDIT: Instead of keeping flag, you can use labels. For example:

loop: while(!flag){
    //take user input here.
    case "John":
        display(12);  //passing value of n
        break loop;

    case "Jenny":
        display(6);
        break loop;
    default:
          System.out.print("Wrong Name");
}

private void display(int n) {
    System.out.println(n) ;
} 
Kaushal28
  • 5,377
  • 5
  • 41
  • 72
  • ok it worked like that thank you very much. But is it possible to use switches and then get the n value in another method. example if the user enters John i want to be able to get the n=12 value and assign it to another variable inside another method within that class. is it possible ? is it even possible to use it in that method it self out of the switch ? – Anjula Serasinghe Mar 14 '18 at 18:09
  • Why not? Just create another method and call it from that particular `switch` statement. – Kaushal28 Mar 14 '18 at 18:15
  • as in ? how do i specifically select the switch statement or case that is selected ? can u give an example through the example iv given ? lets say you want to print the n value inside another method call display ? how is it possible ? sry for the bothering – Anjula Serasinghe Mar 14 '18 at 18:20
  • Neither of your loops has `switch`(es) - this code isn't legal. – Elliott Frisch Mar 14 '18 at 22:39
  • @ElliottFrisch I've just given the example of the flow. – Kaushal28 Mar 15 '18 at 02:24
  • @kaushal28 iv edited the first question into go about what i really want to do because your method is what i like the most. could you take ur time to come about my issue . thank u so much would be of great help – Anjula Serasinghe Mar 16 '18 at 22:49
0

You can use a labeled break instead of the unlabeled https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

getvalidname: // this is a label
while(true){
    //take user input here.
    switch(nameInput) {
        case "John":
            n = 12;
            break getvalidname; // breaks out of the statement just after the label
        case "Jenny":
            n = 6;
            flag = true; 
            break getvalidname;
        default:
            System.out.print("Wrong Name");
    }
}
bcr666
  • 2,157
  • 1
  • 12
  • 23