Java Noob

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2006
Posts: 2
Reputation: Santa_Laws is an unknown quantity at this point 
Solved Threads: 0
Santa_Laws Santa_Laws is offline Offline
Newbie Poster

Java Noob

 
0
  #1
Nov 9th, 2006
Hello, I'm a bit new to Java and I'm wondering how to do arrays with JTextField, so how would i go about starting this program here..

Build a GUI to accept integers (when the ENTER key is depressed) into an array of 15 int; have a button called AVERAGE; when AVERAGE is selected, there is a display of the count and the current average; have a button called CLEAR; when CLEAR is selected, count is set to 0 and so are all the array elements and then there is another display: count is 0 and average is 0.0.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 229
Reputation: DavidRyan is an unknown quantity at this point 
Solved Threads: 11
DavidRyan's Avatar
DavidRyan DavidRyan is offline Offline
Posting Whiz in Training

Re: Java Noob

 
0
  #2
Nov 10th, 2006
The first step is to create a GUI which has what you need.
Then you need to add some listeners to those buttons and keypresses you want the program to respond to.
Then you need to add the programming to those listeners so that the program will do what you intend it to do. In this case add the integers into an array, calculate and display the average, and clear the array.

If you get any errors, feel free to post your code (remember to wrap it in [code]code goes here[/code] tags) and the text of the errors.
Please anyone, correct me if I am wrong. It is the best way for me to learn.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 15
Reputation: fireworks-style is an unknown quantity at this point 
Solved Threads: 0
fireworks-style fireworks-style is offline Offline
Newbie Poster

Re: Java Noob

 
0
  #3
Nov 10th, 2006
Thanks David,

I'm too just learning the ropes with Java. I appreciate your introduction to the subject.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 2
Reputation: Santa_Laws is an unknown quantity at this point 
Solved Threads: 0
Santa_Laws Santa_Laws is offline Offline
Newbie Poster

Re: Java Noob

 
0
  #4
Nov 10th, 2006
  1. package argui;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. /**
  6.  * <p>Title: </p>
  7.  *
  8.  * <p>Description: </p>
  9.  *
  10.  * <p>Copyright: Copyright (c) 2006</p>
  11.  *
  12.  * <p>Company: </p>
  13.  *
  14.  * @author not attributable
  15.  * @version 1.0
  16.  */
  17. public class ArrayGUIPanel extends JPanel
  18. {
  19. private JLabel inputLabel, outputLabel, resultLabel, countLabel, averageLabel;
  20.  
  21. private JTextField nums;
  22. private JButton average, clear;
  23. private int count;
  24. private double avg;
  25. //-----------------------------------------------------------------
  26. // Constructor: Sets up the main GUI components.
  27. //-----------------------------------------------------------------
  28. public ArrayGUIPanel()
  29. {
  30.  
  31. count = 0;
  32. avg=0.0;
  33.  
  34. inputLabel = new JLabel ("Enter integers: " );
  35. resultLabel = new JLabel ("---");
  36. countLabel = new JLabel ("Count: " + count);
  37. averageLabel = new JLabel ("Average: " + avg);
  38.  
  39.  
  40.  
  41. average = new JButton ("AVERAGE");
  42. clear = new JButton ("CLEAR");
  43. AvgListener listener = new AvgListener();
  44. average.addActionListener (listener);
  45. clear.addActionListener(listener);
  46. add (inputLabel);
  47. add (average);
  48. add (clear);
  49. add (resultLabel);
  50. add (countLabel);
  51. add (averageLabel);
  52. setPreferredSize (new Dimension(300, 75));
  53. setBackground (Color.white);
  54.  
  55. }
  56.  
  57. //*****************************************************************
  58. // Represents an action listener for the average input field.
  59. //*****************************************************************
  60. private class AvgListener implements ActionListener
  61. {
  62. //--------------------------------------------------------------
  63. // Performs the conversion when the enter key is pressed in
  64. // the text field.
  65. //--------------------------------------------------------------
  66. public void actionPerformed (ActionEvent event)
  67. {
  68. int numsTemp, averageTemp;
  69. String text = nums.getText();


okay am i going in the right direction here? how does the array come in?
Last edited by Santa_Laws; Nov 10th, 2006 at 1:04 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Java Noob

 
0
  #5
Nov 10th, 2006
The first thing to do is NOT to build that GUI but to figure out what you're actually trying to do.

Without knowing that you can never expect to create a user interface which actually makes sense, let alone the code behind it to work with the data entered and produce the data you want to produce.

Your array is meant to store numbers entered into the user interface for later calculation of their average.
A rather limited implementation, but I guess you haven't come to the point of learning about more appropriate data structures yet so it'll have to do for now.
Just make sure you have some checks on the number of inputs in there so you don't try to insert more than 15 elements into the array
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC