0

I try to create a Java program with GUI and I want one button to generate random numbers and other buttons just do some operations on those random numbers. Currently the size of random number are fixed but in the future the size will be determined by user input. So I decide to use switch-case. It looks like each case has own scope. If I create an array in case 1. I can not reach the array in case 2. I wonder if there are other solutions except creating a globe array. Thanks for the time.

int [] data = new int[100];
switch (index){
  case "1":
    //create array full fill with random number
    for (int i = 0; i< 100; i++){
            data[i] = (int)(Math.random()*(10*100));
        }
    break;
  case "2":
    //sort the array
    sort.(data);
    break;
  default:
    System.exit(0);
}
Jeffery
  • 134
  • 1
  • 2
  • 15
  • what scope do you need for the array? is the question! – ΦXocę 웃 Пepeúpa ツ May 18 '17 at 07:58
  • The switch looks like poor programming. Use a separate [Action](https://docs.oracle.com/javase/8/docs/api/javax/swing/Action.html) for each button, and use the [Random](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html) class to create the numbers in an easier fashion. – Kayaman May 18 '17 at 08:09
  • @Kayaman I would like to hear more about the separate Action for each button. I thought use switch case is separate action. Thanks – Jeffery May 18 '17 at 08:39
  • Using switch case is pretty clumsy when you compare it with [Action or ActionListener](http://stackoverflow.com/questions/27255807/jbutton-action-performed). Separates the functionality nicely. You still need to move the array somewhere where it's accessible to both of the buttons/actions. – Kayaman May 18 '17 at 08:43
  • How will you separates those functionalities? All the actionListener will go to actionPerformed method. Under this method, my thought is using if or switch to determine which button is clicked and click button will call different function with array as parameter. I am not clear the meaning of "separates functionality" – Jeffery May 18 '17 at 08:53
  • You're not supposed to use the same `Action` for both buttons. If you do, *then* you need to do the sillyness with ifs or switches or whatever, and that's just ugly and confusing once you've got more than 2 buttons. – Kayaman May 18 '17 at 08:55
  • "Not use the same Action for both buttons"...let me think about this. Thanks – Jeffery May 18 '17 at 08:57
  • There's nothing to think about. You have 2 buttons, you have 2 actions. Clean and distinct, and you won't need to worry about mixing up the functionality of the buttons. That's the proper way to do it, a switch in a shared `actionPerformed()` wouldn't pass any decent code review. – Kayaman May 18 '17 at 08:58

2 Answers2

0

as you described,the array must exist during two operations, push one button and push another one,which means you have to store the array. Apart from store it in a global array, u can persist it in your files or dbs.

dawnfly
  • 93
  • 2
0

You'll need static int[] data = null;, or create it outside loop. So your switch statement always has access to data array.

switch (index){
    case "1":
        //create array full fill with random number
        int size = 100;
        data = new int[size];
        for (int i = 0; i< size; i++){
            data[i] = (int)(Math.random()*(10*100));
        }
        break;
    case "2":
        //sort the array
        if(data != null)
            sort.(data);
        break;
    default:
        System.exit(0);
}

Additionally, on else conditions of if(data != null) you may want to inform user of You must create array first!.

Sergey
  • 176
  • 2
  • 12