0

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());
1
  • Give an example of what the text looks like since it appears you are trying to convert a single textual integer into an array of integers. Commented Sep 6, 2014 at 14:57

3 Answers 3

1
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]);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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.

e.g.

 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.

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.