简体   繁体   中英

How can I get textbox value into array and convert into integer in java

I need to get textbox value into array and convert them into integer .

I'm not sure whether should I 1st convert and get into array or get into array and after convert .

Please explain with relevant examples I've already tied out this code segment. But its wrong according to my knowledge.

           String data [] = Integer.parseInt(jTextField1.getText());
String[] stringValues = jTextField1.getText().split("[,]");
int[] numArray= new int[stringValues.length];

    for(int i=0; i<numArray.length; i++){
        numArray[i]= Integer.parseInt(stringValues[i]);
    }

You are trying to assign an single inter to a string array so it will not work. Because two types are incompatible.

Either you must have an integer array or you can have string array and use string value of the textfield.

eg

 String []stringData  = {jTextField1.getText()};

or

 int [] = {Integer.parseInt(jTextField1.getText())};

But since you are using just single value it is better to use an variable rather than an array.

Try this:

String str = "34,56,78,32,45";
String[] parts = str.split(",");

for (int i = 0; i < parts.length; i++) 
{  
    int no=Interger.parse(parts[i]);
    //Do your stuff here
}

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