java - char array to int array -
i'm trying convert string array of integers perform math operations on them. i'm having trouble following bit of code:
string raw = "1233983543587325318"; char[] list = new char[raw.length()]; list = raw.tochararray(); int[] num = new int[raw.length()]; (int = 0; < raw.length(); i++){ num[i] = (int[])list[i]; } system.out.println(num); this giving me "inconvertible types" error, required: int[] found: char have tried other ways character.getnumericvalue , assigning directly, without modification. in situations, outputs same garbage "[i@41ed8741", no matter method of conversion use or (!) value of string is. have unicode conversion?
there number of issues solution. first loop condition i > raw.length() wrong - loops never executed - thecondition should i < raw.length()
the second cast. you're attempting cast integer array. in fact since result char don't have cast int - conversion done automatically. converted number isn't think is. it's not integer value expect in fact ascii value of char. need subtract ascii value of 0 integer value you're expecting.
the third how you're trying print resultant integer array. need loop through each element of array , print out.
string raw = "1233983543587325318"; int[] num = new int[raw.length()]; (int = 0; < raw.length(); i++){ num[i] = raw.charat(i) - '0'; } (int : num) { system.out.println(i); }
Comments
Post a Comment