简体   繁体   中英

how do I change a user input string (jtextfield) into an int variable?

I am doing my first java program with a GUI. I need to change the input from the user entered into a jTextField to a int variable i can use for work.

获取文本并使用Integer.parseInt(此处为您的String);

int a = Integer.parseInt(jtextfield.getText());
// `jtextfield` will be your `JTextField` object
    JTextField jTextField=new JTextField(); // initialize textFild
    String str=jTextField.getText(); // get text value 

Now you can convert this into int value.

    int val=Integer.parseInt(jTextField.getText());

jtextfield.gettext() will return the entered String.. we can parse this string into integer

integer.parseInt(txtfield.getText());

but if we are typing values other than numbers 0-9 in the text filed it show java.lang.NumberFormatException

only numbers can be parsed into the integer

int num=Integer.parseInt("2")// is correct

int num=Integer.parseInt("two")//this will give numberformat exception
int result = Integer.parseInt(jTextField.getText());

and apply

class MyIntFilter extends DocumentFilter {
   @Override
   public void insertString(FilterBypass fb, int offset, String string,
         AttributeSet attr) throws BadLocationException {

      Document doc = fb.getDocument();
      StringBuilder sb = new StringBuilder();
      sb.append(doc.getText(0, doc.getLength()));
      sb.insert(offset, string);

      if (test(sb.toString())) {
         super.insertString(fb, offset, string, attr);
      } else {
         // warn the user and don't allow the insert
      }
   }

   private boolean test(String text) {
      try {
         Integer.parseInt(text);
         return true;
      } catch (NumberFormatException e) {
         return false;
      }
   }

   @Override
   public void replace(FilterBypass fb, int offset, int length, String text,
         AttributeSet attrs) throws BadLocationException {

      Document doc = fb.getDocument();
      StringBuilder sb = new StringBuilder();
      sb.append(doc.getText(0, doc.getLength()));
      sb.replace(offset, offset + length, text);

      if (test(sb.toString())) {
         super.replace(fb, offset, length, text, attrs);
      } else {
         // warn the user and don't allow the insert
      }

   }

   @Override
   public void remove(FilterBypass fb, int offset, int length)
         throws BadLocationException {
      Document doc = fb.getDocument();
      StringBuilder sb = new StringBuilder();
      sb.append(doc.getText(0, doc.getLength()));
      sb.delete(offset, offset + length);

      if (test(sb.toString())) {
         super.remove(fb, offset, length);
      } else {
         // warn the user and don't allow the insert
      }

   }
}

(This class copied from Link )

PlainDocument doc = (PlainDocument) jTextField.getDocument();
doc.setDocumentFilter(new MyIntFilter());

This way you can restrict your input to numbers and get the string to integer by parsing it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM