Add user input to ArrayList

I want to add a double that a user inputs to an array list

Page 1 of 1

5 Replies - 8443 Views - Last Post: 29 December 2010 - 08:45 PM Rate Topic: -----

#1 new2Java11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-December 10

Add user input to ArrayList

Posted 29 December 2010 - 01:58 PM

I am trying to code a JApplet where a user can enter a value in to a Jtextfield, then press an enter button to add that value to an arraylist of already entered values. The applet will then update a Jlabel with the new average of those numbers.

My code thus far is fairly primitive, I am just experimenting on a small scale right now before I polish everything up.

Here is my code


/**
 * @(#)Example.java
 *
 * Example Applet application
 *
 * @author
 * @version 1.00 2010/12/29
 */

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
import java.text.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import java.text.DecimalFormat;


public class Example extends JApplet implements ActionListener  {

	Image logo;
	JTextField inputValue;
	JButton enter;
	JLabel label1, label2;
	Double newData;
	double average, avg;
	int sz, q;
	DecimalFormat cents;
	JPanel panel;
	ArrayList<Double> data;



	public void init() {




		data = new ArrayList<Double>();

		data.add(new Double(147.25));
		data.add(new Double(224.35));
		data.add(new Double(264.82));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(25.30));
	/*	if (q==1){

			data.add(new Double(newData));

		}*/ //*TRIED THIS BUT TO NO AVAIL*

		sz = data.size();


		for(double grd:data) {
		avg += grd;
		}



		average = avg/data.size();
		cents = new DecimalFormat ("##.00");

		inputData = new JTextField ("Enter Data");
		inputData.setBackground(Color.BLACK);
		inputData.setForeground(Color.WHITE);
		inputData.setBounds(70, 60, 100, 20);

		enter = new JButton ("Enter");
		enter.setBounds(70, 80, 100, 20);
		enter.addActionListener(this);

		label1 = new JLabel ("");
		label1.setBackground(Color.BLACK);
		label1.setForeground(Color.WHITE);
		label1.setVisible(true);
		label1.setBounds(70, 100, 100, 20);



		panel = new JPanel();
		panel.setLayout(null);
		panel.add(inputTips);
		panel.add(enter);
		panel.add(label1);
		add(panel);

	}

	public void changeInput (){
		String inputTipsinput = inputData.getText();
		newData = Double.parseDouble(inputTipsinput);

	}

	public void actionPerformed(ActionEvent e){

		if (e.getSource() == enter){



			data.add(new Double(newData));
			q=1;
                        //label1.setText(""+cents.format(average));  //Doesn't update



		}

		repaint();
	}

	public void setLabel (){

		if (q==1);
		label1.setText(""+cents.format(average)); //this doesn't work either

	}


	public void paint(Graphics g) {



		super.paint(g);
                label1.repaint();
	}

}




As you can see, have tried a few things, the user input data does not get added, and if it did, the label is not updated. I wonder if there is something wrong with my ActionPerformed method. Any help would be greatly appreciated.

Thanks in advance, I will be very generous with kudos because I am so stuck!

Is This A Good Question/Topic? 0
  • +

Replies To: Add user input to ArrayList

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Add user input to ArrayList

Posted 29 December 2010 - 02:10 PM

First you do not neeed to overload the paint() method changing the text of a JLabel invokes repaint on it anyhow. Second, even if you want to keep it, your label.repaint() is also useless because super.paint(g); did it for you

Not a good idea to put "Enter data:" in your JTextField because when you will read it you will have to remove the "Enter data:" from the String returned by the JTextField
Better to have a JLabel containing "Enter data:" and just beside it a JTextField to enter the number

In your actionPerformed() you have to retreive the String from the JTextField and translate it to double before pushing it into the arrayList

public void actionPerformed(ActionEvent e) {
   if(e.getsource() == enter) {
     String text = inputValue.getText();
     double x = Double.parseDouble(text);    // assuming user enter valid value
     data.add(new Double(x));
     inputValue.setText("");     // clear the data
   }
}



To compute don't forget to set it to 0 first before the loop
then after the loop avg /= data.size();

Happy coding
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Add user input to ArrayList

Posted 29 December 2010 - 02:14 PM

You are making this whole process way to complicated. All you need is one array and simply set the average whenever you press the enter button using setText(). HEre is the reduced code. I got rid of tons of unnecessary variables and methods.

public class Example extends JApplet implements ActionListener  {

	Image logo;
	JTextField inputValue;
	JButton enter;
	JLabel label1, label2;
	Double newData;
	JPanel panel;
	ArrayList<Double> data;



	public void init() {

		data = new ArrayList<Double>();

		data.add(new Double(147.25));
		data.add(new Double(224.35));
		data.add(new Double(264.82));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(310.25));
		data.add(new Double(25.30));

		inputValue = new JTextField ("Enter Data");
		inputValue.setBackground(Color.BLACK);
		inputValue.setForeground(Color.WHITE);
		inputValue.setBounds(70, 60, 100, 20);

		enter = new JButton ("Enter");
		enter.setBounds(70, 80, 100, 20);
		enter.addActionListener(this);

		label1 = new JLabel ("");
		label1.setBackground(Color.BLACK);
		label1.setForeground(Color.WHITE);
		label1.setVisible(true);
		label1.setBounds(70, 100, 100, 20);



		panel = new JPanel();
		panel.setLayout(null);
		panel.add(inputValue);
		panel.add(enter);
		panel.add(label1);
		add(panel);

	}

	public void actionPerformed(ActionEvent e){

		if (e.getSource() == enter){
			data.add(Double.parseDouble(inputValue.getText()));
			label1.setText(takeAverage() + "");
		}
	}
	
	double takeAverage() {
		int sum = 0;
		for (double d : data) {
			sum += d;
		}
		
		return sum / data.size();
	}

}




Notice that you reset the label anytime you press enter? Important concept.
Was This Post Helpful? 1
  • +
  • -

#4 new2Java11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-December 10

Re: Add user input to ArrayList

Posted 29 December 2010 - 05:27 PM

Both excellent solutions guys, THANK YOU SO MUCH. The only thing now is, how do I get the applet to remember where it left off the last time. For instance, given the user inputs a new piece of data every time they run the applet, the first time, there should be 14 pieces of data, (hard programmed in) then every time they press enter, that number should grow by one. This is the case with the current code, however, when I restart the compiler every time it resets to the original 14. Maybe this will change once the program is actually run on the internet?
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Add user input to ArrayList

Posted 29 December 2010 - 05:35 PM

This is called persistence and you are increasing the level of difficulty by a lot. Because it's an applet, you cannot write a file to the user's computer without getting it signed. You are limited with what you can do. However, look into the Applet Persistence API:
http://download.orac...ersistence.html
Was This Post Helpful? 1
  • +
  • -

#6 new2Java11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-December 10

Re: Add user input to ArrayList

Posted 29 December 2010 - 08:45 PM

View PostDogstopper, on 29 December 2010 - 04:35 PM, said:

This is called persistence and you are increasing the level of difficulty by a lot. Because it's an applet, you cannot write a file to the user's computer without getting it signed. You are limited with what you can do. However, look into the Applet Persistence API:
http://download.orac...ersistence.html


Yeah I think I will save this for another day, Thanks again for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1