i have a feild where the user must insert only integer values not chars..if they insert a char an error messege should appear..how can i do this and what is the code to do it

You can test this using a loop which goes through the numbers: "0123456789" If this condition is valid then proceed to the next part of the program. For this you can use a key listener.

actually how do i write the code..i dont know anything abt keylister i am a newbie for java
can u give me a sample code

actually how do i write the code..i dont know anything abt keylister i am a newbie for java
can u give me a sample code

Something like this?

KeyView.java

import javax.swing.*; 
  import java.awt.event.*; 
 import java.awt.*; 
  
  class KeyView extends JFrame implements KeyListener { 
     JTextField keyText = new JTextField(80); 
     JLabel keyLabel = new JLabel("Type in a number mo fo."); 
 
     KeyView() { 
         super("KeyView"); 
         setSize(350, 100); 
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         keyText.addKeyListener(this); 
         Container pane = getContentPane(); 
         BorderLayout bord = new BorderLayout(); 
         pane.add(keyLabel, BorderLayout.NORTH); 
         pane.add(keyText, BorderLayout.CENTER); 
         setContentPane(pane); 
         setVisible(true); 
     } 
 
     public void keyTyped(KeyEvent input) { 
         char key = input.getKeyChar();
         if (key >='0'&& key <='9')
         {
            keyLabel.setText("You pressed " + key); 
         }
         else
             keyLabel.setText("I said press a freaking number?");
     } 
 
     public void keyPressed(KeyEvent txt) { 
         // do nothing 
     } 
 
     public void keyReleased(KeyEvent txt) { 
         // do nothing 
     } 
 
    public static void main(String[] arguments) { 
        
         try {
       UIManager.setLookAndFeel(
           "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );


      } catch (Exception e) { }

     new KeyView(); 
     } 
 }

Or you could wait till the user enters the entire expression and parse it at the end?

hai, check this code and hope this helps u.

/*in ur main program where u have ur textfield, declare the TextField as follows*/

JTextField txtInput =username = new JTextField(new FixedNumericDocument(4,true),"",15);

/*where 4 is the maximum number of digits that is allowed, boolean true denotes whether u need the text box to accept only integers and false otherwise, and 15 is the size of the text field.*/

/* Then as a seperate class call the following code*/

///////FixedNumericDocument.java////////
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class FixedNumericDocument extends PlainDocument {
private int maxLength = 9999;
private boolean numericOnly;
public FixedNumericDocument(int maxLength, boolean numericOnly) {
super();
this.maxLength = maxLength;
this.numericOnly = numericOnly;
}
//this is where we'll control all input to our document.
//If the text that is being entered passes our criteria, then we'll just call
//super.insertString(...)
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (getLength() + str.length() > maxLength) {
Toolkit.getDefaultToolkit().beep();
return;
}
else {
try {
if (numericOnly) {
//check if str is numeric only
Integer.parseInt(str);
//if we get here then str contains only numbers
//so it's ok to insert
}
super.insertString(offset, str, attr);
}
catch(NumberFormatException exp) {
Toolkit.getDefaultToolkit().beep();
return;
}
}
return;
}
}

This article has been dead for over six months. Start a new discussion instead.