Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array with actionPerformed and JTextArea

Posted on 2004-10-30
8
Medium Priority
?
540 Views
Last Modified: 2008-03-03
     Hi there!
  My name is Minh. I'm currently learning Java. I'm doing on a problem dealing with Array. I couldnt make my method in actionPerformed to work, and pass new value in JTextArea. Could you spend sometime to take a look at my code to see what's my problem.

  Ex: Use a one-dimensional array to solve the following problem: A company pays its sales people on a commission basis. The salespeople receive $200 a week and 9% of their gross sales for that week. Write an applet (using an array of counters ) that determines how many of the salespeople earned in each of the following ranges:
 200-299, 300-399, 400-499, 500-599. 600-699. 700-799, 800-899, 900-999, 1000 and over.

  Here is my code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.*;
public class Exercise7_10 extends javax.swing.JApplet implements ActionListener{
    //declare instance variables
    String input, output = "Range of sales\t\t" + "Number of salers\n", display;
    double sales, bonus;
    JLabel salesLbl;
    JTextField salesText;
   
    //declare two arrays
    String range[] = {"$200 - 299", "$300 - 399", "$400 - 499", "$500 - 599", "$600 - 699", "$700 - 799", "$800 - 899", "$900 - 999",
                      "$1000 and over"};
    int counter[] = new int[9];
   
    public void init(){
    //Create the GUI
   
    Container container = getContentPane();
    container.setLayout (new FlowLayout());
   
    //Create and add Components
    salesLbl = new JLabel ("Total sale of the week: ");
    container.add (salesLbl);
    salesText = new JTextField(10);
    container.add (salesText);
    salesText.addActionListener (this);
   
   
    JTextArea outputArea = new JTextArea (15, 30);
    JScrollPane scroller = new JScrollPane (outputArea);
       
    container.add (scroller);
    for (int i = 0; i < counter.length; i++){
           
               
        output += range[i] + "\t\t" + counter[i] + "\n";
           
    }//end for        
   
    outputArea.setText (output);
     
   
   
    }//end method init()
 
   
    public void actionPerformed(ActionEvent e) {
       
       
            input = salesText.getText();
            sales = Double.parseDouble (input);
            bonus = bonuscal (sales);
            if (bonus >= 200 && bonus <= 299){
                counter[0] += 1;  
            }//end if
            if (bonus >= 300 && bonus <= 399){
                counter[1] += 1;  
            }//end if
            if (bonus >= 400 && bonus <= 499){
                counter[2] += 1;  
            }//end if
            if (bonus >= 500 && bonus <= 599){
                counter[3] += 1;  
            }   //end if
            if (bonus >= 600 && bonus <= 699){
                counter[4] += 1;  
            }//end if
            if (bonus >= 700 && bonus <= 799){
                counter[5] += 1;  
            }//end if
            if (bonus >= 800 && bonus <= 899){
                counter[6] += 1;  
            }//end if
            if (bonus >= 900 && bonus <= 999){
                counter[7] += 1;  
            }//end if
            if (bonus >= 1000){
                counter[8] += 1;  
            }//end if        
       
     
    }// end method actionPerformed
   
   
     
    double bonuscal (double sales){
        double bonussale;
        bonussale = 200 + .09 * sales;
        return bonussale;
       
    }//end method bonuscal
   
   public void paint (Graphics g) {
        super.paint (g);
   }
}//end class exercise7_10

Thank you and I really appreciate your time on my problem. Have a good time.
                                                                                             Minh.
0
Comment
Question by:Megrapay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12456208
>>Ex: Use a one-dimensional ... 1000 and over

Is that the *full* spec you've been given?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12456830
Hi Mihn, Vietnamese?

This is homework so you'll get only the skeleton of a solution:
whenever you must count into categories, you must create 2 1-dimewnsional arrays:
The first one with ascending 'edges', the second one to count in.
The algorithm says:
for every value, find the array member, with an edge just above the value,
use its index to count 1 into the second array.

Please come back if you have problems after that.
Chao.
;JOOP!
0
 

Author Comment

by:Megrapay
ID: 12457859
To CEHJ: Yeah, that's about full spec. I cut off the example how to calculate the bonus.
 To sciuriware: It's Minh, yeah, I'm Vietnamese. I'm actually trying to do it myself, thanks for the hint. If you look at my code, I created two 1-dim arrays, one with ascending edges and the other to count in ( range[] and counter[]). For every value, I use the method in actionPerformed to count 1 in the counter array. My problem was I couldnt make that method work to pass 1 to counter[i]. It leaded to that I couldnt make the JTextArea of output change after every value. Could you just look at my method and tell me something about it?
 Thanks for comment.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 24

Accepted Solution

by:
sciuriware earned 2000 total points
ID: 12457907
Well you added an actionlistener to a textfield; why?
The action of filling the textarea should not be there as ....... there is no action!
So there is no display.
Just fill the textfield from the array, and do fill the array in a loop, not with somany if/then/elses.

And sorry for misspelling your name.

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12458125
Personally i would use a separate class to generate the salespeoples earnings randomly
0
 

Author Comment

by:Megrapay
ID: 12458219
 To sciuriware:
 I added an actionlistener to a textfield because I think the value is passed by event handler.
 I tried to modify my code, but I couldnt fill the textare anywhere else accept in the method init(). Could you be tell me a little bit more?
 I put block of "if" in the for loop, I couldnt think of any way not to do the if when I try to find the edge of particular bonus. Thanks.
  To CEHJ:
 Thanks, but I'm just the beginner, I'm afraid I dont know enough to know what you're talking about.
0
 

Author Comment

by:Megrapay
ID: 12458322
Okay, good news: I got it work for the whenever I entered new value. The 1st one works fine, the counter of the edges had value 1 and display. However, for the 2nd value, I dont know how to clear the JTextArea to fill the new one in. My output just keeps make the Applet longer with new output combine with old ones. Here is my code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.*;
public class Exercise7_10 extends javax.swing.JApplet implements ActionListener{
    //declare instance variables
    String input, output = "Range of sales\t\t" + "Number of salers\n", display;
    double sales, bonus;
    JLabel salesLbl;
    JTextField salesText;
   
    //declare two arrays
    String range[] = {"$200 - 299", "$300 - 399", "$400 - 499", "$500 - 599", "$600 - 699", "$700 - 799", "$800 - 899", "$900 - 999",
                      "$1000 and over"};
    int counter[] = new int[9];
   
    public void init(){
    //Create the GUI
   
    Container container = getContentPane();
    container.setLayout (new FlowLayout());
   
    //Create and add Components
    salesLbl = new JLabel ("Total sale of the week: ");
    container.add (salesLbl);
    salesText = new JTextField(10);
    container.add (salesText);
    salesText.addActionListener (this);
   
   
   
     
   
   
    }//end method init()
 
   
    public void actionPerformed(ActionEvent e) {
       
       
        input = salesText.getText();
        sales = Double.parseDouble (input);
        bonus = bonuscal (sales);    
           
       
           
            if (bonus >= 200 && bonus <= 299){
                counter[0] += 1;  
            }//end if
            if (bonus >= 300 && bonus <= 399){
                counter[1] += 1;  
            }//end if
            if (bonus >= 400 && bonus <= 499){
                counter[2] += 1;  
            }//end if
            if (bonus >= 500 && bonus <= 599){
                counter[3] += 1;  
            }   //end if
            if (bonus >= 600 && bonus <= 699){
                counter[4] += 1;  
            }//end if
            if (bonus >= 700 && bonus <= 799){
                counter[5] += 1;  
            }//end if
            if (bonus >= 800 && bonus <= 899){
                counter[6] += 1;  
            }//end if
            if (bonus >= 900 && bonus <= 999){
                counter[7] += 1;  
            }//end if
            if (bonus >= 1000){
                counter[8] += 1;  
            }//end if                
               
           
           
                 
       for (int i = 0; i < counter.length; i++){
         
               
            output += range[i] + "\t\t" + counter[i] + "\n";
           
       }//end for        
       Container container = getContentPane();
       container.setLayout (new FlowLayout());
       JTextArea outputArea = new JTextArea (15, 30);
       container.add (outputArea);
       outputArea.setText (output);
   
       
   
        salesText.setText ("");
        salesText.requestFocus ();
    }// end method actionPerformed
   
   
     
    double bonuscal (double sales){
        double bonussale;
        bonussale = 200 + .09 * sales;
        return bonussale;
       
    }//end method bonuscal
   
 
       
}//end class exercise7_10
  I'm still trying to figure it out to fix that small problem left. Thanks for your comment. It helped.
 
0
 

Author Comment

by:Megrapay
ID: 12458588
 I got it fixed, thanks a lot guys.
  To sciuriware: Your suggestion really helped, thank to you.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question