Please! can anyone help me on this, Please!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Please! can anyone help me on this, Please!

Hybrid View

  1. #1
    Join Date
    Aug 2004
    Location
    Philippines
    Posts
    17

    Unhappy Please! can anyone help me on this, Please!

    Hi!,
    Please! anyone help me on this and If anyone read this please help me on this and I don't know what is the problem on this and here is an sample output. Please!, help me on this.

    Sample Output:
    Enter Month Code: 1
    January

    Enter Month Code: a
    Letter is Not Accepted!!!


    Here is the Java Code:

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


    public class MonthApp extends Applet implements ActionListener
    {
    Label lmonth=new Label("Enter Month Code:");
    TextField tmonth=new TextField(3);
    Font large=new Font("Tahoma",Font.BOLD,15);
    Button displaymonth=new Button("Display Month!!!");

    public void init()
    {

    add(lmonth);
    add(tmonth);
    add(displaymonth);
    displaymonth.addActionListener(this);
    }
    public void actionPerformed(ActionEvent AlcioAbaquita)
    {
    int monthcode=Integer.parseInt(tmonth.getText());
    Label month=new Label("");
    Label text=new Label("");
    String displaybutton=displaymonth.getLabel();
    switch (monthcode)
    {
    case 1:
    month.setText("January");
    month.setFont(large);
    add(month);
    break;
    case 2:
    month.setText("February");
    month.setFont(large);
    add(month);
    break;
    case 3:
    month.setText("March");
    month.setFont(large);
    add(month);
    break;
    case 4:
    month.setText("April");
    month.setFont(large);
    add(month);
    break;
    case 5:
    month.setText("May");
    month.setFont(large);
    add(month);
    break;
    case 6:
    month.setText("June");
    month.setFont(large);
    add(month);
    break;
    case 7:
    month.setText("July");
    month.setFont(large);
    add(month);
    break;
    case 8:
    month.setText("August");
    month.setFont(large);
    add(month);
    break;
    case 9:
    month.setText("September");
    month.setFont(large);
    add(month);
    break;
    case 10:
    month.setText("October");
    month.setFont(large);
    add(month);
    break;
    case 11:
    month.setText("November");
    month.setFont(large);
    add(month);
    break;
    case 12:
    month.setText("December");
    month.setFont(large);
    add(month);
    break;
    default:
    month.setText("Invalid Month Code!!!");
    month.setFont(large);
    add(month);
    text.setText("Letter is Not Accepted!!!");
    text.setFont(large);
    add(text);
    }

    validate();
    }
    }

  2. #2
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Well, you're only telling your applet to display a moth if the input is a number between 1 and 12. If the input is 'a', it does the default thing in the switch statement.

  3. #3
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    What is the program doing incorrectly? Or what do you want it to do that it is not doing?
    It looks like it is working correctly.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Where are you generating your action event? I don't find anything in your code which would send a "AlcioAbaquita" message to your ActionListener.

    Also - you have a lot of repeating code (all of the display stuff). Can you think of a way to decompose that?

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    What's with the pile of labels ?

    I have restructure and enhanced this a bit. Also, I changed the logic that
    repeatedly created new labels and added them, didn't look good
    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class MonthApp
        extends Applet
        implements ActionListener {
    
      Label month = new Label("");
      Label text = new Label("");
      Label lmonth = new Label("Enter Month Code:");
      TextField tmonth = new TextField(3);
      Font large = new Font("Tahoma", Font.BOLD, 15);
      Button displaymonth = new Button("Display Month!!!");
    
      String [] months={
        "January","February","March","April","May","June",
        "July","August","September","October","November", "December"
      };
    
      public void init() {
        add(lmonth);
        add(tmonth);
        add(displaymonth);
        add(month);
        add(text);
        displaymonth.addActionListener(this);
        month.setFont(large);
        text.setFont(large);
      }
    
      public void actionPerformed(ActionEvent ae) {
        clearMsg();
    
        String entry=tmonth.getText().trim();
        try {
          int monthcode = Integer.parseInt(entry);
          getMonthByNumber(monthcode);
        }
        catch (NumberFormatException ex) {
          getMonthByString(entry);
        }
        validate();
      }
      /**
       * Get a month by character match
       */
      private void getMonthByString(String entry) {
        // check for sensible string input
        for (int i=0; i<months.length; i++) {
          if (months[i].toLowerCase().startsWith(entry.toLowerCase())) {
            getMonthByNumber(i+1);
            return;
          }
        }
        issueErrorMsg();
      }
      /**
       * Get a month by number
       */
      private void getMonthByNumber(int monthcode) {
    
        if (monthcode >=1 && monthcode <= 12) {
          month.setText(months[monthcode-1]);
        } else {
          issueErrorMsg();
        }
      }
    
      private void issueErrorMsg() {
        month.setText("Invalid Month Code!!!");
        text.setText("Input is Not Accepted!!!");
      }
      private void clearMsg() {
        month.setText("");
        text.setText("");
      }
    
      /**
       * Test driver for applet
       * @param args
       */
      public static void main (String [] args) {
        Frame f=new Frame();
        f.setLayout(new GridLayout());
        MonthApp ma=new MonthApp();
        f.add(ma);
        f.setBounds(20,20,600,400);
        f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        ma.init();
        f.setVisible(true);
      }
    
    }
    Last edited by sjalle; 09-18-2005 at 07:59 PM.
    eschew obfuscation

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center