ppra
ppra

Reputation: 31

subtract integer from a string of integers

Suppose I have a string array as follows:

   String[] str = {"2","4","5"};

Now I want to subtract 1 from each of its elements, ie, I want the string to be like this now:

    str = {"1","3","4"};

How do I do it? Is there any way other than converting it into an integer array?

Upvotes: 0

Views: 9433

Answers (8)

logbasex
logbasex

Reputation: 2242

You can use org.apache.commons.lang3.math library to solve it in an elegant way.

for(int i = 0; i < str.length; i++) {
  str[i] = NumberUtils.toInt(str[i]) - 1;
}

Upvotes: 0

Code
Code

Reputation: 184

 public static void main(String[] args) throws IOException {

    String str[] = subtractOn(new String[]{"2","4","5"});

    for(int k=0;k<str.length;k++){
        System.out.println("Integer is :" +str[k]);
    }

  }

public static String[] subtractOn(String str[]){

    int intArray[] = new int[str.length];
    String stres[] = new String[str.length];

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

    for(int j=0;j<intArray.length;j++){
        stres[j] = String.valueOf(intArray[j]-1);
    }

    return stres;
}

Upvotes: 0

Matthias Meid
Matthias Meid

Reputation: 12513

You have to convert them into integers, but not necessarily store into an array of integers. You can do the math in-place instead:

for(int i = 0; i < str.length; i++) {
  str[i] = Integer.toString(Integer.parseInt(str[i]) - 1);
}

However, this is a code smell to me. Strings do not tend to be the best choice when doing math in general. You might also want to work with an int[] internally and convert them to strings when needed.

Upvotes: 2

Aswin
Aswin

Reputation: 1204

Hope this will helps you.

public String[] stringCal(String[] ele,int numbr){
    String[] sCalulated = new String[ele.length];
    for(int i = 0; i < ele.length ; i ++){
        sCalulated[i] = String.valueOf(Integer.parseInt(ele[i])-numbr);
    }
    return sCalulated;
}

Upvotes: 0

lunr
lunr

Reputation: 5269

for (int i = 0; i < str.length; i++)
    str[i] := String.valueOf(Integer.parseInt(str[i]) - 1);
}

Upvotes: 0

Rohan
Rohan

Reputation: 3078

Try this,

String str[]= {"2","4","5"};
for(int i=0;i<str.length;i++)
{
str[i]=String.valueOf(Integer.parseInt(str[i])-1));
}

Upvotes: 2

Lithu T.V
Lithu T.V

Reputation: 20021

  1. convert string to int : int foo = Integer.parseInt("1234");
  2. Subtract 1 from it
  3. Convert back to string Integer.toString(i)

That makes

for (int i = 0; i < strn.length; i++)
    strn[i] := String.valueOf(Integer.parseInt(strn[i]) - 1);
}

Upvotes: 0

SubSevn
SubSevn

Reputation: 1028

You would need to convert them to Integers.

If you could make some crazy constraints, you could get it a little better, for example... Only having single digit integers in an array of characters, strictly greater than zero. You could then do the "math" by subtracting 1 from their ASCII value, but this is a pretty crazy situation to even ever have.

Upvotes: 1

Related Questions