This is my second week learning Java and I'm trying to implement some functions that I saw in the book I'm studying with.
With the first function I wanted to represent an integer as an array of integer digits so that I can bypass the range of integer values.
For example:
1st Function
Input : integerInput(123456)
Output : integerString[] = {1,2,3,4,5,6}
2nd function
Input : stringInput[] = {123456}
Output : integerString[] = {1,2,3,4,5,6}
3rd function:
(I want to add the 2 integer string values as in like normal addition)
Input : integerString1[] = {1,2,3,4,5,6} integerString2[] = {1,2,3,4,5,6}
Output : [2,4,6,9,1,2]
This is what I have so far, it is full of mistakes and all i could get is an empty output. Would appreciate any help.
Thanks.
public class tryingOut{
public static int[] integerToNumberArray(int value) {
String temp = Integer.toString(value);
int[] list = new int[temp.length()];
for (int i = 0; i < temp.length(); i++) {
list[i] = temp.charAt(i) - '0';
}
return list;
}
public static boolean isNumeric(String value) {
if (value == null) {
return false;
}
try {
int d = Integer.parseInt(value);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
public static int[] stringToNumberArray(String value) {
boolean checkNumeric = isNumeric(value);
if (checkNumeric = false) {
return null;
} else {
String[] items = value.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "").split(",");
int[] results = new int[items.length];
for (int i = 0; i < items.length; i++) {
try {
results[i] = Integer.parseInt(items[i]);
} catch (NumberFormatException nfe) {
}
}
return results;
}
}
public static int[] addNumberArrays(int[] a1, int[] a2) {
int[] result = null;
return result;
}
public static void main(String[] args) {
int test1 = 19;
int test2 = 823;
int[] list1 = integerToNumberArray(test1);
int[] list2 = integerToNumberArray(test2);
// stringToNumberArray(test1);
// stringToNumberArray(test2);
}
}
BigIntegerclass built into Java.