1

in my Android app I have a set of string array values - tvs[1], tvs[2], tvs3[3], etc., which contain the textview value from a number of Number Pickers. However, I need the Integer value of each of these strings, so I can then multiply this numeric value with the unit cost of various meals. ie 4 x burgers, 3 x chips, etc. How do I go about this process? I've looked at other questions, but can't find the answer. Can anyone suggest a solution? ie int Value = .....(tvs[1]

user1641906
  • 117
  • 2
  • 13

2 Answers2

2

you can simply convert your whole string array to integer array.

int[] integersArray = new int[stringArray.length];
for(int i = 0;i < stringArray.length;i++){
   integersArray [i] = Integer.parseInt(stringArray[i]);
}
Faris Jameel
  • 576
  • 2
  • 5
  • 22
1

This is just one of many ways on how to convert a String to int.

int a = Integer.valueOf(string);
Bojan Kseneman
  • 14,663
  • 2
  • 50
  • 58
  • Thanks - but what is the syntax for using this, say, in a Toast ie using tvs[1] – user1641906 May 20 '15 at 18:48
  • This is what I have at the moment: result.append(tvs[1].getText().toString() + "x" + "\nHaddock(Med) 50Rs"); - but I want to multiply the 50Rs by the integer value from the string array element tvs[1] – user1641906 May 20 '15 at 18:51
  • total += Integer.valueOf(tvs[1].getText().toString()) * 50 – Bojan Kseneman May 20 '15 at 18:58
  • Hi again! Your answer worked prefectly - one more query (final one!) - how do I add the BigDecimal totalamount1 to totalamount+= , as below. – user1641906 May 21 '15 at 11:04
  • BigDecimal bd = new BigDecimal("4.4"); BigDecimal value = new BigDecimal (Integer.valueOf(tvs[1].getText().toString())); BigDecimal totalamount1 = value.multiply(bd); totalamount+= totalamount1; – user1641906 May 21 '15 at 11:04
  • Compiler doesn't like it!! But I want to add the result of my arithmetic to the running total.. – user1641906 May 21 '15 at 11:05
  • It's the line totalamount+= totalamount1 it doesn't like - the += causes a mismatch.. – user1641906 May 21 '15 at 11:07
  • Are you sure you have big enough numbers to need big decimal? Simple double is more than good enough in most cases. See here how to add stuff to the big decimal http://stackoverflow.com/questions/1846900/addition-for-bigdecimal – Bojan Kseneman May 21 '15 at 11:07
  • Ah thanks - so it looks like I just need Double - ie 5 x £4-40, 3 x £1-20 etc.! – user1641906 May 21 '15 at 11:19
  • Yes, big decimal is only useful for big numbers as it's name suggests, – Bojan Kseneman May 21 '15 at 11:34
  • Initialized with: double totalamount=0.0; and then, double d1 = 4.4; double d2 = Integer.valueOf(tvs[1].getText().toString()); totalamount+= d1 * d2; - BUT on using 3 in NumberPicker/TextView, totalamount came out as 0.0 - Any idea why?? Thanks! – user1641906 May 21 '15 at 11:34
  • Try Double.valueOf(tvs[1].getText().toString()); ... integer is not for decimal numbers – Bojan Kseneman May 21 '15 at 11:36
  • Just realised - I was using wrong Number Picker with item in list!! – user1641906 May 21 '15 at 11:40
  • Works well now - except result is £13.200000000 - how do I curtail number of decimal places in result?? Many thanks!! – user1641906 May 21 '15 at 11:41
  • http://stackoverflow.com/questions/8065114/how-to-print-a-double-with-two-decimals-in-android – Bojan Kseneman May 21 '15 at 11:43