It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Character length problem

Character length problem

Postby cascade on Sat Feb 17, 2007 7:43 pm

Hi people. i want to do a project to test win95 file name lengths of 8 charcters but i'm having problems

limiting the length of characters to 8.
Can someone tell me what i'm doing wrong?
thanks in advance. btw, this will compile to test the calculations and the button 'fi' is for the result

to be displayed.

Code: Select all
/*start with a letter but should contain a mix of letters and digits upto 8 characters
*decimal point can have three digits or letters
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;


public class RecogniseNumbers3 extends java.applet.Applet
                    implements ActionListener
{
    final int IDLE = 0;
    final int BUILDING_REAL = 1;
    final int BUILDING_INT = 2;
    final int INVALID = 3;
    int state;//current user holds state
    Panel ip;//contains user buttons
    TextField input;//displays current number
    TextField result;//displays result of number check
    Label nameinput = new Label();//new label object
    Label nameresult = new Label();//new label object
       
    StringBuffer currentinput;//input characters stored here
    char currentchar;
   

     
   
   
    public void init()
    {   //------------------------------------------//
        //       Layout Manager for the labels      //
        //------------------------------------------//
        Panel p2 = new Panel();//new panel object p2 for labels
        p2.setLayout(new GridLayout(2,1,2,2));
        add("West", p2);//add the labels to the west
        Label Labelinput = new Label("Filename: ");
        p2.add(Labelinput);
        Label Labelresult = new Label("Result: ");
        p2.add(Labelresult);
       
        //--------------------------------------//
        //    BorderLayout for the Textfields   //
        //--------------------------------------//
        // create a 2 x 1 grid for the displays and add it to the north
        // section of the root panel
        Panel p = new Panel();//new panel object 'p' textfields
        p.setLayout(new GridLayout(2,1,2,2));//set a grid layout for the panel
        add("East", p);//add panel in the north
       
        // create the displays and add them to the 2 x 1 grid
        input = new TextField(24);//textfield size
        input.setEditable(false);//read only
        p.add(input);//add input textfield
       
        // create the displays and add them to the 2 x 1 grid
        result = new TextField(24);//textfield size
        result.setEditable(false);//read only
        p.add(result);//add result textfield
       
       
        //------------------------------------------//
        //          Gridlayout for the panel        //
        //------------------------------------------//
        Panel ip = new Panel();//input panel for the keypad
        ip.setLayout(new GridLayout(4,3,4,4));//gridlayout for the keypad
        Button b;
        for (char input='a'; input < 'z' + 1; input++)//make buttons for a to z
        {
            b = new Button ("" + input);//send button request
            b.addActionListener(this);
            ip.add(b);
        }//end for loop
        for (char input='0'; input < '9' + 1; input++ )//make button from 0 to 9
        {
            b = new Button ("" + input);//send button request
            b.addActionListener(this);
            ip.add(b);//add buttons inside the input panel object
        }//end for loop
       
        b = new Button(".");
        b.addActionListener(this);
        ip.add(b);
       
        b = new Button("fi");
        b.addActionListener(this);
        ip.add(b);
       
        add("South", ip);//adds the Input panel(ip) object to the south
        currentinput = new StringBuffer();
        state = IDLE;//initial state
    }//end init

    public void handleChar()
    {
       
        switch(state)
            {
            case IDLE:
                    result.setText("");//clear result of any display
                    input.setText("");//clear any previous display
                    if (currentchar == '.')//decimal point so must be real
                    {
                        state = BUILDING_REAL;
                        currentinput.append(currentchar);
                        //add to the input string
                       
                        input.setText(currentinput.toString());
                        //refresh output window
                       
                        break;
                    }
               
                    else//must be a digit
                    {
                        state = BUILDING_INT;
                        currentinput.append(currentchar);
                        input.setText(currentinput.toString());
                        break;
                    }
            case BUILDING_REAL:
            {
                       
                if (currentchar == '.')//cant have two
                 
                {
                    state = INVALID;
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
                }
                else
                {
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
                }
               
            }
            case BUILDING_INT:
                if (currentchar == '.')
                {
                    state = BUILDING_REAL;
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
                }
                else
                {
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
                }
            case INVALID:
                    currentinput.append(currentchar);
                    input.setText(currentinput.toString());
                    break;
        }//end switch
           
    }//end handleChar
       
   

    public void finish()
    {
        switch(state)
        {
            case BUILDING_REAL:
                 result.setText("Valid");
                 state = IDLE;
                 currentinput = new StringBuffer();
                 //discard old string bufffer
                 break;
                 
           case BUILDING_INT:
                 result.setText("The number is an integer");
                 state = IDLE;
                 currentinput = new StringBuffer();
                 break;
                 
            case INVALID:
                 result.setText("Invalid");
                 state = IDLE;
                 currentinput = new StringBuffer();
        }//end switch
    }//end finish
 
    public void actionPerformed(ActionEvent event)
    {
        String command = event.getActionCommand();
        if (event.getSource() instanceof Button)
        {
            if (command.equals("fi"))
            {
                finish();
            }
            else
            {
                currentchar = command.charAt(0);
                handleChar();
            }
        }
    }
}
       

cascade
 
Posts: 2
Joined: Sun Feb 04, 2007 6:44 pm

Who is online

Users browsing this forum: No registered users and 0 guests