6 Replies - 164 Views - Last Post: 04 February 2012 - 06:31 PM Rate Topic: -----

Topic Sponsor:

#1 Arete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-January 12

Saving an array to a specific path.

Posted 04 February 2012 - 04:52 PM

Hey guys I am looking for some help on an assignment. I have to:

Add a Save button( Yes or No) to the GUI that saves the content of the array to a C:\data\Weight.dat file.

I have been using JOptionPane throughout multiple different variations of this program so a little late to change as this is due tomorrow. So I setup a JOptionPane.ConfirmDialog (hope that is correct). I am utterly confused on how to assign the "yes" button to save to the path listed above.

Here is my code:

package guiprogrampart2; 

import java.awt.*;
import javax.swing.*;
import java.net.*; 
import java.io.*;

    public class GuiProgramPart2{

    public static void main(String[] args) throws Exception{
            //pulls icon from Web for display on MessageDialog
            final ImageIcon icon = new ImageIcon(new URL("http://www.veryicon.com/icon/preview/Nature/Solar%20System/Earth%20Icon.jpg"));
            String[] name = new String[4]; 
            double[] weight = new double[4];
            Weight [] object = new Weight[4];
            UIManager cl = new UIManager ();
            File inputFile = new File("C:\\data\\Weight.dat");
            int ObjectWeight;
            

            for (int i = 0; i <object.length; i++)
            {
            boolean error = false;
            //request the name of an object from user
            String ObjectName = JOptionPane.showInputDialog("What is the Objects name?"); 
            do {
            //request the weight of an object
            ObjectWeight = Integer.parseInt(JOptionPane.showInputDialog( "What is the Objects Weight?"));
             
                //determines if user entered a correct number
                if( ObjectWeight > 99999 || ObjectWeight < 0 )
                    {     
                        cl.put("OptionPane.messageForeground", Color.red);
                    //error message determined by IF statement variables
                    JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 99,999", "Error", JOptionPane.ERROR_MESSAGE);
                    error = true;
                    } 
                
                    else
                        { 
                        error = false;
                        }
                }
                   while( error ); 
                
            object[i] = new Weight();
            object[i].setObjectName(ObjectName);
            object[i].setWeight(ObjectWeight);
            
             cl.put("OptionPane.messageForeground", Color.blue);  
           
             
                       // display result in a JOptionPane message dialog
            JOptionPane.showMessageDialog( null, "The " + ObjectName + " weighs " + ObjectWeight + " lbs on Earth" + "\nOn the Moon it weighs " + object[i].calcMoon() + " lbs "+ "\nOn Mercury it weighs " + object[i].calcMercury() + " lbs "+ "\nOn Jupiter it weighs " + object[i].calcJupiter() + " lbs ","Weight of Object",JOptionPane.INFORMATION_MESSAGE, icon );
            }    
            JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);
                int confirm = JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);
                  
    
    }
 }


The changes I made to original was adding:

            JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);
                int confirm = JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);


and

File inputFile = new File("C:\\data\\Weight.dat");


and I am at a loss on how to proceed. Hours of research online and through what books I have handy have proved useless. Any help would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Saving an array to a specific path.

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 719
  • View blog
  • Posts: 1,692
  • Joined: 05-May 05

Re: Saving an array to a specific path.

Posted 04 February 2012 - 05:52 PM

If you look at the API it says showConfirmDialog returns an integer indicating the option that was chosen. That integer is a static public field of the JOptionPane class.

if(confirm == JOptionPane.OK_OPTION){ /* save to file */}


To do file I/O you need a stream. File is a meta-object that represents a system file, but lacks the ability to do I/O. You have several choices: FileWriter, BufferedWriter, FileOutputStream, and PrintWriter. I suggest using a FileWriter, which you can instantiate with your File instance. Remember to close the stream before exiting the main method.

You also might want to consider using a DataOutputStream.

This post has been edited by blackcompe: 04 February 2012 - 05:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is online

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: Saving an array to a specific path.

Posted 04 February 2012 - 05:55 PM

So you've been using JOptionPanes to get input from the user, but you're not sure how to turn the "Yes" button press into an action that saves the file? Do I have that right?

If I'm close, I borrowed the following example from the Java tutorials, a file called "DialogDemo.java":


int n = JOptionPane.showConfirmDialog(
        frame, "Would you like green eggs and ham?",
        "An Inane Question",
        JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
    setLabel("Ewww!");
} else if (n == JOptionPane.NO_OPTION) {
    setLabel("Me neither!");
} else {
    setLabel("Come on -- tell me!");
}

Notice how int n is assigned to capture the button press, and then evaluated to decide what to do based on which button was pressed.

Does that make sense? The tutorial is here, if you want to review it yourself.
Was This Post Helpful? 0
  • +
  • -

#4 Arete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-January 12

Re: Saving an array to a specific path.

Posted 04 February 2012 - 06:10 PM

Correct, I am unsure how to associate the action of clicking yes to saving the file to C:\data\Weight.dat file.

I understand the IF and ELSE statements and that they end up determining which way the program goes, however I am confused on how to associate a yes click with saving...

so I would have:

if(confirm == JOptionPane.OK_OPTION){ //here is where it would direct the action to the file path i assume?   }


Just not sure what is to be included in the {} for the save function.

I have actually looked over that tutorial several times throughout the weeks building up to this product :phone:
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 719
  • View blog
  • Posts: 1,692
  • Joined: 05-May 05

Re: Saving an array to a specific path.

Posted 04 February 2012 - 06:13 PM

Quote

I understand the IF and ELSE statements and that they end up determining which way the program goes, however I am confused on how to associate a yes click with saving...


Actually, as Greg pointed out, you should use JOptionPane.YES_OPTION. But, yes, you should put your file I/O code in that block. If you need help with file I/O look at the Java tutorials.

This post has been edited by blackcompe: 04 February 2012 - 06:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 Arete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-January 12

Re: Saving an array to a specific path.

Posted 04 February 2012 - 06:22 PM

Right so I have the:

            JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);
                int confirm = JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);
                  if(confirm == JOptionPane.OK_OPTION)


Now when you say I/O code are you referring to this:

File inputFile = new File("C:\\data\\Weight.dat");


I ask you to forgive me if this are some extremely "nub" questions, first class ever dealing with Java. Lots of reading and practice and more reading...stuff can get jumbled and confused sometimes....
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 719
  • View blog
  • Posts: 1,692
  • Joined: 05-May 05

Re: Saving an array to a specific path.

Posted 04 February 2012 - 06:31 PM

Quote

I ask you to forgive me if this are some extremely "nub" questions, first class ever dealing with Java. Lots of reading and practice and more reading...stuff can get jumbled and confused sometimes....


I know what you mean.

File I/O involves using a stream. You've got a file only. The File class is only good for creating, deleting, and getting information on files. See the I/O tutorials.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1