-2

Hey you guys looking for some help! I need to be able to reference and isolate strings from a list in order to convert them into variable names.

String[] planeTitles = new String[] {"Focke-Wulf Fw 190", "Messerschmitt Bf 109","Messerschmitt Me 262", "Supermarine MKs 24 Spitfire",
                                "Yakovlev Yak-3", "Vought F4U Corsair", "Lockheed P-38 Lightning", "North American P-51 Mustang", "Mitsubishi A6M Zero"};


JComboBox<String> planeList = new JComboBox<>(planeTitles);


          add(planeList);

In order to draw from that list I am using...

String selectedPlane = (String) planeList.getSelectedItem();

For example I need to be able to isolate "Focke-Wulf Fw 190" from the list when the user selects it in the box and convert it to equal...

double fw190; 

I am not trying to change names of variables necessarily but just get the string to a point where I can assign a value for data comparison later.

Any help is appreciated!!!

  • Possible duplicate of [Creating a variable name using a String value](http://stackoverflow.com/questions/8631935/creating-a-variable-name-using-a-string-value) – Yassin Hajaj May 17 '16 at 05:29
  • 1
    "*While you can do what you're trying in some scripting languages such as PHP (and this question is often asked by many PHP programmers who start Java), this is not how Java works, and in fact variable names are a much less important than you may realize and hardly even exist after code is compiled.*" – Yassin Hajaj May 17 '16 at 05:29
  • Thanks for your response, while I see what you're re getting at it hardly answers my question. Is what I'm doing possible? I don't literally need to change any names but just to assign values to the strings in order to later compare data. – Ferrariman11 May 17 '16 at 05:41
  • I don't see the necessity to create dynamic variables from String. But you can use a HashMap which keys will be the selected elements and value would be a Double value. – tfosra May 17 '16 at 06:06
  • "For example I need to be able to isolate "Focke-Wulf Fw 190" from the list when the user selects it in the box and convert it to a double." How? What part of that String converts to a double value? The 190? – Gilbert Le Blanc May 17 '16 at 06:39
  • The Fw 190 is a plane. I just tried using 'double fw190 = Double.parseDouble("Focke-Wulf Fw 190");' but I cant seem to get that to work. – Ferrariman11 May 17 '16 at 06:48

1 Answers1

0

You may consider to create an enumeration with each element having both a string-representation (to be used within the combo) and a value for data comparison.

public enum PlaneEnum
{
  FOCKE_WULF_190("Focke-Wulf Fw 190", 190),
  MESSERSCHMITT_109("Messerschmitt Bf 109", 109),
  ....

  public String toString()
  {
    return (myDescr);
  }

  public double getValue()
  {
    return (myValue);
  }

  private PlaneEnum(String description,
                    double value)
  {
    myDescr = description;
    myValue = value;
  }

  private String myDescr;
  private double value;

} // enum PlaneEnum

JComboBox<PlaneEnum> = new JComboBox<PlaneEnum>(PlaneEnum.values);
Robert Kock
  • 5,795
  • 1
  • 12
  • 20