DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Sep 2010
    Posts
    0

    Question Array: button problem need help.

    Need some help regarding my code. Button btnMin not functioning well. When I try to click btnFill then btnMin, the button works fine, but when I try to click the four buttons consecutively, btnMin failed to work. Hope you can help me.

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.util.*;  
    
    public class array extends Applet implements ActionListener
    {
        Button btnFill = new Button("Fill Array");
        Button btnSort = new Button("Sort");
        Button btnMax = new Button("Find Maximum");
        Button btnMin = new Button("Find Minimum");
        TextArea Display = new TextArea(10,20);
        Label lblMaxMin = new Label("##################");
        int[] myArray = new int[5];
        int x;
        String strOutput = "";
    
    public void init()
    {
        Panel pnl1 = new Panel();
        Panel pnl2 = new Panel();
        Panel pnl3 = new Panel();
    
        pnl1.setLayout(new GridLayout(4,0));
        pnl1.add(btnFill);    btnFill.addActionListener(this);
        pnl1.add(btnSort);    btnSort.addActionListener(this);
        pnl1.add(btnMax);    btnMax.addActionListener(this);
        pnl1.add(btnMin);    btnMin.addActionListener(this);
    
        pnl2.setLayout(new GridLayout(1,1));
        pnl2.add(Display);
    
        pnl3.setLayout(new GridLayout(6,0));
        pnl3.add(lblMaxMin);
        add(pnl1);
        add(pnl2);
        add(pnl3);
    }
    
    public void actionPerformed(ActionEvent a)
    {
        Object Obj = a.getSource();
    
        if(Obj == btnFill)
        {
          Display.setText("");
          for(int x=0; x<myArray.length; x++)
          {
            myArray[x] = (int)(Math.random()*100)+1;
            Display.append("[" + (x+1) + "]" + "   " + myArray[x] + "\n");
          }
        }
    
        else if(Obj == btnSort)
        {
          Display.setText("");
          Arrays.sort(myArray);
          for(int x=0; x<myArray.length; x++)
          {
            Display.append("[" + (x+1) + "]" + "   " + myArray[x] + "\n");
          }
        }
    
        else if(Obj == btnMax)
        {
          lblMaxMin.setText("");
          int max = myArray[0];
          for(int x=0; x<myArray.length; x++)
          {
            if(myArray[x] > max)
            {
            max = myArray[x];
            lblMaxMin.setText("Maximum Number =" + max);
            }
          }
        }
    
        else
        {
          lblMaxMin.setText("");
          int min = myArray[0];
          for(int x=0; x<myArray.length; x++)
          {
            if(myArray[x] < min)
            {
            min = myArray[x];
            lblMaxMin.setText("Minimum Number =" + min);
            }
          }
        }
    }
    }

  2. #2
    Join Date
    Oct 2010
    Posts
    10

    replace you code with this

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.util.*;

    public class array extends Applet implements ActionListener
    {
    Button btnFill = new Button("Fill Array");
    Button btnSort = new Button("Sort");
    Button btnMax = new Button("Find Maximum");
    Button btnMin = new Button("Find Minimum");
    TextArea Display = new TextArea(10,20);
    Label lblMaxMin = new Label("##################");
    int[] myArray = new int[5];
    int x;
    String strOutput = "";

    public void init()
    {
    Panel pnl1 = new Panel();
    Panel pnl2 = new Panel();
    Panel pnl3 = new Panel();

    pnl1.setLayout(new GridLayout(4,0));
    pnl1.add(btnFill); btnFill.addActionListener(this);
    pnl1.add(btnSort); btnSort.addActionListener(this);
    pnl1.add(btnMax); btnMax.addActionListener(this);
    pnl1.add(btnMin); btnMin.addActionListener(this);

    pnl2.setLayout(new GridLayout(1,1));
    pnl2.add(Display);

    pnl3.setLayout(new GridLayout(6,0));
    pnl3.add(lblMaxMin);
    add(pnl1);
    add(pnl2);
    add(pnl3);
    }

    public void actionPerformed(ActionEvent a)
    {
    Object Obj = a.getSource();

    if(Obj == btnFill)
    {
    Display.setText("");
    for(int x=0; x<myArray.length; x++)
    {
    myArray[x] = (int)(Math.random()*100)+1;
    Display.append("[" + (x+1) + "]" + " " + myArray[x] + "\n");
    }
    }

    else if(Obj == btnSort)
    {
    Display.setText("");
    Arrays.sort(myArray);
    for(int x=0; x<myArray.length; x++)
    {
    Display.append("[" + (x+1) + "]" + " " + myArray[x] + "\n");
    }
    }

    else if(Obj == btnMax)
    {
    lblMaxMin.setText("");
    int max = myArray[0];
    for(int x=0; x<myArray.length; x++)
    {
    if(myArray[x] > max)
    {
    max = myArray[x];

    }
    }
    lblMaxMin.setText("Maximum Number =" + max);
    }

    else if (Obj==btnMin)
    {
    //lblMaxMin.setText("");
    int min = myArray[0];
    for(int x=0; x<myArray.length; x++)
    {
    if(myArray[x] < min)
    {
    min = myArray[x];

    }
    }
    lblMaxMin.setText("Minimum Number =" + min);
    }
    }
    }

    // just put set text outside the loop

Similar Threads

  1. Replies: 4
    Last Post: 10-23-2011, 02:47 PM
  2. problem loading more array in listview
    By shamsam1 in forum VB Classic
    Replies: 6
    Last Post: 04-23-2008, 10:26 AM
  3. Array analysis problem
    By brouse in forum VB Classic
    Replies: 1
    Last Post: 05-10-2005, 02:19 PM
  4. Radio Button Problem.
    By Ravindra in forum ASP.NET
    Replies: 1
    Last Post: 05-12-2003, 11:56 PM
  5. Array to bitmap problem
    By David Vowles in forum VB Classic
    Replies: 0
    Last Post: 10-12-2001, 12:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links