Results 1 to 13 of 13
  1. #1
    ron_j_m is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Question about a simple Java programing assignment.

    Hello,

    Like many I am new to Java, and programing in general and have a question about an assignment I am working on.
    The program simply adds rainfall data for 30 days to a text file. It must use a submit button and a JTextField, and have a for loop that runs from 1 to 30.
    The problem I am having is with the for loop. I have tried to put it within the actionlistener but it just loops 30 times after the submit button is pressed, and doesn't wait for any more input.
    Here is my code that works fine without a for loop.
    Any sugestions on how to add a for loop that will count to 30?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.util.*;
    import java.io.*;
    
    public class Rainfall extends JFrame implements ActionListener
    {
    	// declare an output stream
    	DataOutputStream output;
    
    	//assign components
    	JLabel headerLabel = new JLabel("30 Day Rainfall Recorder");
    	JLabel rainfallLabel = new JLabel("Input Rainfall Below");
    	JTextField rainfallField = new JTextField(10);
    	Double[] rainfallArray = new Double[30];
    
    	//button
    	JButton submitButton = new JButton("Submit");
    
    	public Rainfall()
    	{
    		super("Rainfall Recorder");
    	}
    
    	public static void main(String[] args)
    	{
    
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		Rainfall r = new Rainfall();
    		r.setSize(200,200);
    		r.createContentPane();
    		r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		r.setVisible(true);
    	}
    
    
    	public void createContentPane()
    	{
    
    		// construct and populate the north panel
    		JPanel northPanel = new JPanel();
    		northPanel.setLayout(new FlowLayout());
    		northPanel.add(headerLabel);
    
    		// construct and populate the center panel
    		JPanel centerPanel = new JPanel();
    		//centerPanel.setLayout(new GridLayout(3,1));
    		centerPanel.add(rainfallLabel);
    		centerPanel.add(rainfallField);
    		centerPanel.add(submitButton);
    
    		// add the panels to the content pane
    		setLayout(new BorderLayout(10, 10));
    		add(northPanel, BorderLayout.NORTH);
    		add(centerPanel, BorderLayout.CENTER);
    		submitButton.addActionListener(this);
    
    		String filename = "rainfall.dat";
    
    		try
    		{
    			output = new DataOutputStream(new FileOutputStream(filename));
    		}
    		catch(IOException io)
    		{
    			JOptionPane.showMessageDialog(null,
    				"The program could not create a file.  Contact your IT Department.",
    				"Error", JOptionPane.INFORMATION_MESSAGE);
    
    			System.exit(1);
        	}
    
    
    	}
    
    
    
    
    	public void actionPerformed(ActionEvent e)
        {
    
    		if ( checkFields() )
    		{
    			try
    			{
    				// save the data
    				output.writeUTF(rainfallField.getText());
    				JOptionPane.showMessageDialog(null,
    				"Rainfall Data Has Been Saved.", "Success", JOptionPane.INFORMATION_MESSAGE);
    			}
    			catch(IOException c)
    			{
    				System.exit(1);
    			}
    
    			clearFields();
    		}
    
    	}
    
    	public boolean checkFields()
    	{
    		if ( ( 	rainfallField.getText().compareTo("") < 1 ) )
    		{
    			JOptionPane.showMessageDialog(null, "You must enter a Rainfall Amount.",
    				"Data Entry Error", JOptionPane.WARNING_MESSAGE);
    			return false;
    		}
    		else
    		{
    			return true;
    		}
        }
    
         public void clearFields()
    	{
    		rainfallField.setText("");
    		rainfallField.requestFocusInWindow();
        }
    }

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    What do you need the loop to do? Update a JLabel?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im not quite sure how exactly a loop can work with a text field? A text field seems like the perfect component for allowing multiple inputs. My approach would be to monitor an instance variable and each button click should do something with the current text value and update the counter. When the counter is 30 it shouldn't do anything else with input.

  4. #4
    ron_j_m is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by ra4king View Post
    What do you need the loop to do? Update a JLabel?
    This is what I don't understand as well.

    Here is the actual assignment.

    Your friend is majoring in meteorology. As a class project, she has been manually recording the daily rainfall amounts from the last 30 days. She new would like to generate an electronic data file to store the information. Create a Java appliaction with a for loop that runs from 1 to 30. Components should include an input JTextField, a submit button, and appropriate promtps. Store the data file with the name RainfallReadings.dat.
    I guess I don't understand the logic behind the for loop specified in this application.

  5. #5
    ron_j_m is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Im not quite sure how exactly a loop can work with a text field? A text field seems like the perfect component for allowing multiple inputs. My approach would be to monitor an instance variable and each button click should do something with the current text value and update the counter. When the counter is 30 it shouldn't do anything else with input.
    This sounds good to me, but I don't understand how the for loop will work propertly. If i have the loop in an action event for the submit button, once the submit button is pressed it will just loop through 30 times without allowing anymore input in the meantime.

    Confused:confused:

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Exactly my point. A textfield allows you to not need a loop. Is there anyway you can get clarification from the teacher?

  7. #7
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You could use JOptionPane. To get inputs you would use JOptionPane.showInputDialog(String message).

  8. #8
    ron_j_m is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by ra4king View Post
    You could use JOptionPane. To get inputs you would use JOptionPane.showInputDialog(String message).
    I thought of that ass well, but then I would not meet the requirement of having a textfield and a submit button.

    I think what I might do is store the values in an array every time the submit button is pressed. Then use a for loop to add the values from the array in a file once all the weather data is put in. It seems this would meet all the requirments, but the logic is pretty stupid.

    Any other ideas?

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    How about manually creating a JDialog and adding the required components to it?

  10. #10
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Wait, you want a textfield and submit button?
    Java Code:
    String input = JOptionPane.showInputDialog("Type in the textfield:");

  11. #11
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Java Code:
    for(int i=0; i<30; i++)
    String input = JOptionPane.showInputDialog("Type in the textfield:");
    try this :)

  12. #12
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    All that will achieve is to show the same dialogue box 30 times, and ignore the first 29 inputs because each time it overwrites the previous one.

  13. #13
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    No it won't, you can process each input at a time.
    Java Code:
    for(int a = 0; a < 30; a++) {
        String input = JOptionPane.showInputDialog("Please type some text:");
        //process
    }

Similar Threads

  1. Simple programing help.
    By koppojutsu in forum New To Java
    Replies: 12
    Last Post: 09-11-2010, 02:28 AM
  2. i have a question on my assignment??(java)
    By javanew in forum New To Java
    Replies: 4
    Last Post: 03-28-2010, 12:15 AM
  3. Replies: 4
    Last Post: 11-02-2009, 04:41 PM
  4. Programing Technique question - Try or If
    By TimHuey in forum New To Java
    Replies: 6
    Last Post: 09-15-2009, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •