Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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.

/*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();
}
}
}
}
[8000 byte] By [cascade] at [2007-11-11 10:24:58]
# 1 Re: character length problem
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 ...
nspils at 2007-11-11 22:31:33 >