1

What would be a better alternative (simplest) to compress my code in the example below?

I'm thinking there could be a way to loop or array these. When I tried to put them in an array, the syntax errors out. Maybe arrayList or treeMap would be a better approach? I'm a little rusty with those structures. Am I looking to deep into this?

private JMenuItem one                      = new JMenuItem("One");
private JMenuItem two                      = new JMenuItem("Two");
//...                                      = ...;
//...                                      = ...;
private JMenuItem sixtySeven               = new JMenuItem("Sixty    Seven");

for(conditions or array looping)
{
    private JMenuItem i = new JMenuItem(i.toString().proper());//I have to research if there is even a "proper" method.
}
//Revised example below:
private JMenu edit                           = new JMenu("Edit");
private JMenu format                         = new JMenu("Format");
private JMenu view                           = new JMenu("View");
private JMenu help                           = new JMenu("Help");
private JMenuItem save                       = new JMenuItem("Save");
private JMenuItem open                       = new JMenuItem("Open");
private JMenuItem exit                       = new JMenuItem("Exit");
Sizzlewump
  • 23
  • 5
  • The method `Integer.toString()` will give you the number with digits, not words -- e.g., "67", not "Sixty-Seven". See also [How to convert number to words](https://stackoverflow.com/questions/3911966/). – Andy Thomas Aug 15 '19 at 15:42
  • 2
    Whether there is a better way to accomplish your goal depends on what you’re actually trying to do. Are you really creating menu items with English spellings of numbers? And if you’re putting 67 menu items in a single menu, there are much better UI idioms for accomplishing that functionality than a giant unwieldy menu… – VGR Aug 15 '19 at 15:45
  • What is the syntax error you get with array ? because it's a very good idea – azro Aug 15 '19 at 15:47

3 Answers3

2

You can't have a for loop outside of methods, and if you move it inside a method, then you'd be declaring local variables, so private wouldn't be allowed.

Instead, use an array:

private JMenuItem[] menuItems = new JMenuItem[67];
{
    // This code is in an initializer block and will
    // run regardless of which constructor is called.
    for (int i = 0; i < menuItems.length; i++) {
        this.menuItems[i] = new JMenuItem(numberToWords(i + 1));
    }
}

private static String numberToWords(int number) {
    // Code here
}

See How to convert number to words in java for implementation of numberToWords.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

One thing you could do is just add each item to an arraylist, that way rather than having 67 variables to keep track of you just have to worry about the one ArrayList.

0

Using Java 8 I would go for Streaming and collecting it afterwards. That should save a lot of lines.

private static final int START = 1;
private static final int END = 67;
private List<JMenuItem> list;


private void initializeList(){

   /*You will need the convertIntToSpelledString(int) method
    *as @Andy Thomas mentioned above in his comment
    */

   this.list = IntStream.rangeClosed(START, END).boxed()
                        .map(i -> new JMenuItem(convertIntToSpelledString(i))
                        .collect(Collectors.toList());
}


kitaird
  • 23
  • 1
  • 8
  • I like the ideas everyone is presenting. I'm not sure if the numberToWords method would be simplifying anything. I was using numbers as an example hoping for a simple solution, but my code is a little more complex. Here is the actual use-case: – Sizzlewump Aug 15 '19 at 17:17
  • code-view private JMenu file = new JMenu("File"); private JMenu edit = new JMenu("Edit"); private JMenu format = new JMenu("Format"); private JMenu view = new JMenu("View"); private JMenu help = new JMenu("Help"); private JMenuItem save = new JMenuItem("Save"); private JMenuItem open = new JMenuItem("Open"); private JMenuItem exit = new JMenuItem("Exit"); – Sizzlewump Aug 15 '19 at 17:18
  • You can allocate the names of the `JFrames` into a seperate property-file and give them a `key` you could iterate like this (by i.e. a alphanumeric key that is countable). However, if this is more of a simple project, you can also create an `EnumClass` to store these specific `names` you want to give your `JFrames` in order to achieve a separation of concerns in terms of `data` and actual `logic` – kitaird Aug 15 '19 at 17:24