flwyd: (java logo)
[personal profile] flwyd
Save me Edsgar! I just wished I could use a goto statement in Java. My code's primary purpose is to take a bunch of fixed-width data and save it to another file in a tab-delimited format. I decided to add the ability to save the layout. If the layout file already exists, I prompt the user with a Yes/No/Cancel dialog to overwrite. If they cancel, I don't want to write anything (layout or data) and just return from the function. If they say no, I won't write the layout file, but I still need to write the data. If they say yes, or if the file didn't already exist, I write the layout file and then write the data. With a goto, I could break out of my if conditions and just write the data. But without the goto, my options are to set a status flag, write a function to prompt and write the layout, returning a boolean if it was canceled so I can refrain from writing data (probably the best solution), or the following solution: write a one-iteration for loop and break to a label, like so:
    if (verifyInputFile() && verifyOutputFile()) {
        if (StringUtilities.isNotEmpty(saveLayoutField.getText())) {
            gotohack: for (int hack = 0; hack == 0; ++hack) {
                File layoutFile = new File(saveLayoutField.getText());
                if (layoutFile.isDirectory()) {
                    layoutFile = new File(layoutFile, viewLinker.getInputFile().getName() + ".layout.txt");
                }
                if (layoutFile.exists()) {
                    int choice = OptionDialog.showStandardYesNoCancelDialog(parent, "Layout file " + layoutFile + " exists.  Overwrite?");
                    if (choice == OptionDialog.CANCEL) {
                        return;
                    }
                    if (choice == OptionDialog.NO) {
                        break gotohack; // because Java can't goto the run call
                    }
                }
                try {
                    PrintWriter writer = new PrintWriter(new FileOutputStream(layoutFile));
                    for (Iterator iter = fieldModel.iterator(); iter.hasNext();) {
                        Field field = (Field) iter.next();
                        writer.println(field.name + "\t" + field.length);
                    }
                    writer.close();
                } catch (Exception e) {
                    parent.handleException("Could not write layout file " + layoutFile, e);
                }
            }
        }
	writeData();
    }

Date: 2006-12-16 03:59 am (UTC)
From: [identity profile] flwyd.livejournal.com
That doesn't work though. Because I only pop up the dialog if the layout file already exists. If it's free, I write without prompt. So the choices are keeping state, using goto, or creating a boolean function which writes the layout (which is the best solution, since the above code is all in a conditional if (verifyInput() && verifyOutput())).

Date: 2006-12-16 04:08 am (UTC)
From: [identity profile] gentleman-lech.livejournal.com
I must have missed that in my initial read through of the code. However, you could still use my solution by initializing the option to YES before checking to see if you need to pop up the dialog. That way if the dialog doesn't pop up, the layout still gets written.

But you're probably right about the boolean function being the best option.
December 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 2025

Most Popular Tags

Expand Cut Tags

No cut tags
Page generated Monday, December 29th, 2025 09:25 am
Powered by Dreamwidth Studios