Array with actionPerformed and JTextArea

     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.
MegrapayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>Ex: Use a one-dimensional ... 1000 and over

Is that the *full* spec you've been given?
0
sciuriwareCommented:
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
MegrapayAuthor Commented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

sciuriwareCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Personally i would use a separate class to generate the salespeoples earnings randomly
0
MegrapayAuthor Commented:
 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
MegrapayAuthor Commented:
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
MegrapayAuthor Commented:
 I got it fixed, thanks a lot guys.
  To sciuriware: Your suggestion really helped, thank to you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.