java - How to concatenate seperate digits from a loop into one single integer -


okay have assignment says have string of numbers example "1234 4014 5555 7654" credit card number. asking me convert string of digits single integers concatenate them 4 lots of 4 digit integers string "1234 4014 555 7654" example have 4 blocks block 1 1234 converted 1, 2, 3, 4 concatenated 1234 integer have blocks... :(

so far.. made loop shown below :

public static int toint(string digitstring) {     int answer = 0;     int num = 0;     (int j = 0; j < digitstring.length(); j++){         num = (int) digitstring.charat(j) - '0';         system.out.println(num);     }      return answer 

}

and can convert string seperate digits have no idea how can concatenate digits 4, 4 digit integers

any appreciated :)

i'm not going assignment you, i'll tell hint: need know understand , solve problem integer.parseint(s) string s returns s integer, , s.substring(n, n+1) returns (n+1)st character of string.

for example

string s = "1234"; s = s.substring(0, 1); //s = "1" int val = integer.parseint(s); //val = 1 

and that's it. it's matter of looping on string , doing whatever want them. suppose might helpful know can assign integer string with:

string temp = val + ""; //or string temp = string.valueof(val); 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -