简体   繁体   中英

Casting A String To An Int From A JTextField In Java

So I'm trying to use the text entered in a TextField as an Int ... heres my code

int command1 = (int) (command.getText());

But I get the error "Cannot cast from string to int" an I can't figure it out... can anyone help?

ps the text in the TextField is not numbers so

int command1 = (int) (Double.parseDouble(command.getText()));

Will not work...

You need to parse the string containing the integer, not simply cast it.

int aInt = Integer.parseInt(aString);

Edit: "PS The Text In The TextField Is NOT Numbers " you can't create an int out of something that is not a number. "ABC" cannot be an int.

使用整数包装器对象...

int iVal = Integer.parseInt(String);

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