character length problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: character length problem

  1. #1
    Join Date
    Feb 2007
    Posts
    5

    character length problem

    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:
    /*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();
                } 
            }
        }
    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    That's all pretty fancy, but I think I would take advantage of methods in the string class (since the filename is a string) - if charAt(0) is not a letter then the file name is illegal, if the entire string is more than 12 characters then it is illegal, search the string for a ".", see what position it is in, if that is beyond 8 then the filename is illegal, if the substring after the "." is longer than 3 then the name is illegal ...

Similar Threads

  1. String length problem
    By jellyfish5000 in forum .NET
    Replies: 2
    Last Post: 02-16-2007, 05:21 AM
  2. Replies: 6
    Last Post: 12-11-2002, 05:08 AM
  3. Reliability Problem
    By elise in forum Java
    Replies: 0
    Last Post: 10-30-2002, 04:40 AM
  4. Changing Array Length Problem
    By Damo in forum ASP.NET
    Replies: 3
    Last Post: 10-15-2002, 02:58 PM
  5. Replies: 1
    Last Post: 08-04-2000, 08:53 AM

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