Solved

Array with actionPerformed and JTextArea

Posted on 2004-10-30
512 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
Question by:Megrapay
    8 Comments
     
    LVL 86

    Expert Comment

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

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

    Expert Comment

    by:sciuriware
    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
    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
     
    LVL 24

    Accepted Solution

    by:
    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
    Personally i would use a separate class to generate the salespeoples earnings randomly
    0
     

    Author Comment

    by:Megrapay
     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
    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
     I got it fixed, thanks a lot guys.
      To sciuriware: Your suggestion really helped, thank to you.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    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…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    This video teaches viewers about errors in exception handling.

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now