Am I adding items to my Java array wrong? - java

public class ProjectEulerProb2 {
int firstInt = 1, secondInt = 2, thirdInt = 0, answer = 0;
int[] array = new int[4000000];
int[] evenArray = new int[90];
public static void main(String args[]) {
ProjectEulerProb2 prob = new ProjectEulerProb2();
prob.doIt();
prob = null;
}
public void doIt() {
for (int i = 0; i <= 4000000; i++) {
if (i == 0) {
thirdInt = firstInt + secondInt;
}
else {
firstInt = secondInt;
secondInt = thirdInt;
thirdInt = firstInt + secondInt;
}
array[i] = firstInt;
array[i + 1] = secondInt;
array[i + 2] = thirdInt;
if (thirdInt >= 4000000) {
break;
}
}
for (int j = 0; j <= 90; j = j + 3) {
if (j == 0) {
if (array[j + 1] % 2 == 0) {
System.out.println(" " + array[j + 1] % 2 + " " + array[j + 1]);
evenArray[j / 3] = array[j + 1];
}
if (array[j + 2] % 2 == 0) {
System.out.println(" " + array[j + 2] % 2 + " " + array[j + 2]);
evenArray[j / 3] = array[j + 2];
}
}
if (array[j] % 2 == 0) {
System.out.println(" " + array[j] % 2 + " " + array[j]);
evenArray[j / 3] = array[j];
}
}
for (int u = 0; u < evenArray.length; u++) {
if (u == 0) {
answer = evenArray[u];
}
else {
answer = answer + evenArray[u];
}
}
System.out.println(answer);
}
}
Could someone please help me find the problem? Every time I print the values of the array it comes out as 0 instead of the assigned value.
EDIT: Okay I took all the 'System.out.println's'
out that I didn't need.
EDIT 2: Okay so I rewrote the code to not use arrays anymore. Still interested in figuring out where I went wrong with the last version though.
public class ProjectEulerProb2Other {
static int firstInt=1, secondInt=2, thirdInt=0, answer=0;
public static void main(String[] args){
for(int i = 0; i<=4000000;i++){
if(i==0){
if(firstInt%2==0){
answer = answer+firstInt;
}
if(secondInt%2==0){
answer = answer+secondInt;
}
thirdInt = firstInt+secondInt;
}else{
firstInt = secondInt;
secondInt = thirdInt;
thirdInt = firstInt+secondInt;
if(thirdInt%2==0){
answer = answer+thirdInt;
}
}
if(thirdInt>=4000000){
System.out.println(answer);
break;
}
}
}
}

There is no problem with how you add elements to your array.
array[i] = firstInt;
Is correct. However, there are problems in your logic. Since this is probably an homework, I'll let you find them :)
EDIT
Okay so the problem was in this loop in your first version:
for (int j = 0; j <= 90; j = j + 3) {
if (j == 0) { //Bad idea!!
if (array[j + 1] % 2 == 0) { //Always false. array[0 + 1] == 1
System.out.println(" " + array[j + 1] % 2 + " " + array[j + 1]);
evenArray[j / 3] = array[j + 1];
}
if (array[j + 2] % 2 == 0) { //Always true. array[0 + 2] == 2
System.out.println(" " + array[j + 2] % 2 + " " + array[j + 2]);
evenArray[j / 3] = array[j + 2];
}
}
if (array[j] % 2 == 0) {
System.out.println(" " + array[j] % 2 + " " + array[j]);
evenArray[j / 3] = array[j];
}
}
My fix:
int jCpt = 0; //To add in evenArray in an orderly manner
for (int j = 0; jCpt < 90 && j < 4000000; ++j) { //Changed this
if (array[j] % 2 == 0) {
System.out.println(" " + array[j] % 2 + " " + array[j]);
evenArray[jCpt++] = array[j]; //We add alement #j from array to evenArray
/* Equivalent of
* evenArray[jCpt] = array[j];
* jCpt = jCpt + 1;
*/
}
}
But that version is probably better:
int evenCpt = 0; //To insert the even numbers one after the other
int fibonacciCpt = 0; //To iterate through the fibonacci numbers in array
for (; evenCpt < 90 && fibonacciCpt < 4000000; ++fibonacciCpt) {
if (array[fibonacciCpt] % 2 == 0)
evenArray[evenCpt++] = array[fibonacciCpt];
}
Congratulations, you solved Problem #2 :)

Related

Why do I get java.lang.ArrayIndexOutOfBoundsException here?

public long process(long[][] theArray) {
long result = 0l;
int xDimension = 0;
int yDimension = 0;
for (int i = 0; i < theArray.length; i++) {
for (int j = 0; j < theArray[0].length; j++) {
if (((yDimension + 12) < theArray.length) && ((xDimension + 12) < theArray[0].length)) {
result = (theArray[yDimension][xDimension + 1]) + (theArray[yDimension][xDimension + 2])
+ (theArray[yDimension + 1][xDimension]) + (theArray[yDimension + 1][xDimension + 3])
+ (theArray[yDimension + 2][xDimension]) + (theArray[yDimension + 2][xDimension + 3])
+ (theArray[yDimension + 3][xDimension + 1]) + (theArray[yDimension + 3][xDimension + 2]);
}
xDimension++;
}
xDimension -= (theArray[0].length);
yDimension++;
}
return result;
}// method()
--> my console says : Index 5 out of bounds for length 5. But how is that possible together with my if-condition?
xDimension starts with 0 and will be negative after one iteration of the outer loop.

Two dimensional array input from user in java from a sequence of lines, ending with a line input "end"

Here is my code. I think that there is much better way to fill matrix with integers from String lines. My output shows correct output, but it is too complicated. How to make it less complex?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int [][]matrix = new int [7777][7777];
int counter = 0;
int counter1 = 0;
for (int i = 0; i < 7777; i++) {
String s = scanner.nextLine();
if (!"end".equals(s)) {
counter++;
String s1[] = s.split(" ");
for (int j = 0; j < s1.length; j++) {
matrix[i][j] = Integer.parseInt(s1[j]);
counter1++;
}
} else {
break;
}
}
int v = counter;
int h = counter1/counter;
for (int i = 0; i < v; i++) {
for (int j = 0; j < h; j++) {
if (v == 1 || h == 1) {
System.out.print(matrix[i][j]*4 + " ");
} else if (i == 0){
if (j == 0){
System.out.print(matrix[v-1][j] + matrix[i+1][j] + matrix[i][h-1] + matrix[i][j+1] + " ");
} else if (j != h-1){
System.out.print(matrix[v-1][j] + matrix[i+1][j] + matrix[i][j-1] + matrix[i][j+1] + " ");
} else {
System.out.print(matrix[v-1][j] + matrix[i+1][j] + matrix[i][j-1] + matrix[i][0] + " ");
}
} else if (j == 0 && i != v-1){
System.out.print(matrix[i-1][j] + matrix[i+1][j] + matrix[i][h-1] + matrix[i][j+1] + " ");
} else if (j != 0 && j != h-1 && i != v-1) {
System.out.print(matrix[i-1][j] + matrix[i+1][j] + matrix[i][j-1] + matrix[i][j+1] + " ");
} else if (j == h-1 && i != v-1){
System.out.print(matrix[i-1][j] + matrix[i+1][j] + matrix[i][j-1] + matrix[i][0] + " ");
} else if (i == v-1) {
if (j == 0) {
System.out.print(matrix[i-1][j] + matrix[0][j] + matrix[i][h-1] + matrix[i][j+1] + " ");
} else if (j != h-1) {
System.out.print(matrix[i-1][j] + matrix[0][j] + matrix[i][j-1] + matrix[i][j+1] + " ");
} else {
System.out.print(matrix[i-1][j] + matrix[0][j] + matrix[i][j-1] + matrix[i][0] + " ");
}
}
}
System.out.println();
}
}
}
Here is the task assignment.
Write a program, which inputs the rectangular matrix from a sequence of lines, ending with a line, containing the only word "end" (without the quotation marks).
The program should output the matrix of the same size, where each elements in the position (i, j) is equal to the sum of the elements from the first matrix on the positions (i-1, j), (i+1, j), (i, j-1), (i, j+1). Boundary elements have neighbours on the opposite side of the matrix. In the case with one row or column, the element itself maybe its neighbour.
Sample Input:
9 5 3
0 7 -1
-5 2 9
end
Sample Output:
3 21 22
10 6 19
20 16 -1
Yes this code could be simplified.
Starting off, using array is not the best choice for containing the input because you don't know the input size. Using a List that can expand to fit the data will be easier. Further, using the Stream api, we can convert the input into a List<List<Integer>> fairly easily.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
List<List<Integer>> matrix = reader.lines()
.takeWhile(line -> !line.equals("end"))
.map(line -> Arrays.stream(line.split(" "))
.map(Integer::valueOf)
.collect(Collectors.toList()))
.collect(Collectors.toList());
Now that we have the data in our list we will compute the size of the width and height.
int height = matrix.size();
int width = matrix.get(0).size();
Computing the locations of (i-1, j), (i+1, j), (i, j-1), (i, j+1) without an IndexOutOfBoundsException is a little more tricky, however you can use this modulo formula. As long as the offset isn't negatively larger than the size this will work. You can also that the modulo of the offset if that is a concern
(size + index + offset) % size
// or
(size + index + (offset % size)) % size
To add the strings together with a space you can use a StringJoiner.
for (int i = 0; i < height; i++) {
StringJoiner joiner = new StringJoiner(" ");
for (int j = 0; j < width; j++) {
int value = matrix.get((height + i - 1) % height).get(j)
+ matrix.get((height + i + 1) % height).get(j)
+ matrix.get(i).get((width + j - 1) % width)
+ matrix.get(i).get((width + j + 1) % width);
joiner.add(String.valueOf(value));
}
System.out.println(joiner.toString());
}

Crossword algorithm for Android using Java [duplicate]

This question already has answers here:
Algorithm to generate a crossword [closed]
(13 answers)
Closed 6 years ago.
I am working on cross word algorithm to develop a word app. After doing a lot of googling or search on StackOverflow, I was able to reach this point. But yet I am not able to understand the right implementation for algorithm in Java. Below is the class I used.
public class Crosswords {
char[][] cross;
int rows;
int cols;
char[][] numberGrid;
boolean startword;
final char DEFAULT = ' ';
public Crosswords() {
rows = 50;
cols = 50;
cross = new char[rows][cols];
numberGrid = new char [rows][cols];
for (int i = 0; i < cross.length;i++){
for (int j = 0; j < cross[i].length;j++){
cross[i][j] = DEFAULT;
}
}
}
public Crosswords(int ros, int colls) {
rows = ros;
cols = colls;
cross = new char[rows][cols];
numberGrid = new char [rows][cols];
for (int i = 0;i < cross.length; i++){
for (int j = 0; j < cross[i].length; j++){
cross[i][j] = DEFAULT;
}
}
}
public String toString() {
String s = new String();
//String d = new String();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++){
s = s + cross[i][j] + " ";
}
s = s + "\n";
}
return s;
}
public void addWordh(String s, int r, int c) {
int i = 0;
int j = 0;
boolean b = true;
boolean intersectsWord = true;
if (s.length() > cols) {
System.out.println(s + " is longer than the grid. Please try another word.");
return;
}
if (c + s.length() > cols) {
System.out.println(s + " is too long. Please try another word.");
return;
}
if ((r - 2) >= 0) {
if ((cross[r - 1][c - 1 + s.length()] == DEFAULT) || (cross[r - 1][c - 1 + s.length()] == '*')) {
intersectsWord = false;
}
else { intersectsWord = true;}
if (intersectsWord == true) {
System.out.println("The word " + s + " intersects the beginning of another word!");
return;
}
}
for (i = 0; i < s.length(); i++) {
if ((cross[r - 1][c - 1 + i] == DEFAULT) || (cross[r - 1][c - 1 + i] == s.charAt(i))) {
b = true;
}
else {
b = false;
System.out.println("Unable to add " + s + ". Please try another word.");
return;}
}
if (b == true) {
if ((s.length() <= cols) && (c + s.length() <= cols) &&
(cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {
while (j < s.length()) {
cross[r - 1][c - 1 + j] = s.charAt(j);
if (j==0){
startword = true;
}
cross[rows - 1 - (r - 1)][cols - 1 - (c - 1 + j)] = '*';
j++;
}
}
}
}
public void addWordv(String s, int r, int c) {
int i = 0;
int j = 0;
boolean b = true;
boolean intersectsWord = true;
if (s.length() > rows) {
System.out.println(s + " is longer than the grid. Please try another word.");
}
if (r + s.length() > rows) {
System.out.println(s + " is too long. Please try another word.");
}
else {
if ((r - 2) >= 0) {
if ((cross[r - 2][c - 1] == DEFAULT) || (cross[r - 2][c - 1] == '*')) {
intersectsWord = false;
}
else { intersectsWord = true;}
if (intersectsWord == true) {
System.out.println("The word " + s + " intersects the end of another word!");
return;
}
}
if ((cross[r - 1 + s.length()][c - 1] == DEFAULT) || (cross[r - 1 + s.length()][c - 1] == '*')) {
intersectsWord = false;
}
else { intersectsWord = true;}
if (intersectsWord == true) {
System.out.println("The word " + s + " intersects the end of another word!");
return;
}
for (i = 0; i < s.length(); i++) {
if ((cross[r - 1 + i][c - 1] == DEFAULT) || (cross[r - 1 + i][c - 1] == s.charAt(i))) {
b = true;
}
else {
b = false;
System.out.println("Unable to add " + s + ". Please try another word.");
return;}
}
if (b == true) {
if ((s.length() <= rows) && (r + s.length() <= cols) &&
(cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {
while (j < s.length()) {
cross[r - 1 + j][c - 1] = s.charAt(j);
if (j==0){
startword = true;
}
cross[rows - 1 - (r - 1 + j)][cols - 1 - (c - 1)] = '*';
j++;
}
}
}
}
}
public void setNumberGrid(){
numberGrid = new char [rows][cols];
for (int i = 0; i < cross.length; i++){
for (int j=0; j < cross[rows].length; j++){
if (cross[i][j] == DEFAULT){
numberGrid[i][j] = (char) 0;
}
else if (startword == true){
numberGrid[i][j] = (char) -2;
}
else {
numberGrid[i][j] = (char) -1;
}
}
int count = 1;
for (i=0; i < cross.length; i++){
for (int j=0; j < cross[rows].length; j++){
if (numberGrid[i][j] == -2){
numberGrid[i][j] = (char)count;
count++;
}
}
}
}
}
public String printNumberGrid() {
for (int i=0; i < cross.length; i++){
for (int j=0; j < cross[rows].length; j++){
if (numberGrid[i][j] == (char)-1){
numberGrid[i][j] = ' ';
}
else if (numberGrid[i][j] == (char)0){
numberGrid[i][j] = '#';
}
}
}
String d = new String();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++){
d = d + numberGrid[i][j] + " ";
}
d = d + "\n";
}
return d;
}
public static void main(String[] args) {
Crosswords g = new Crosswords();
g.addWordv("rawr", 4, 5);
g.addWordh("bot", 5, 4);
g.addWordv("raw", 7, 5);
g.addWordh("cat", 4, 5);
g.addWordh("bass", 6, 10);
System.out.println(g);
Crosswords c = new Crosswords(20, 20);
c.addWordh("HELLO", 1, 1);
c.addWordv("HAPLOID", 1, 1);
c.addWordh("COMPUTER", 3, 12);
c.addWordv("CAT", 2, 11);
c.addWordv("WOAH", 2, 20);
c.addWordh("PARKING", 20, 5);
c.addWordv("ARK", 17, 6);
c.addWordh("AHOY", 6, 18);
c.addWordv("AHOY", 18, 10);
c.addWordv("ADVANTAGE", 2, 12);
c.addWordv("INTERNAL", 2, 18);
c.addWordh("BANTER", 7, 11);
c.addWordv("BEAGLE", 5, 12);
c.addWordh("BASE", 8, 3);
c.addWordv("BALL", 8, 3);
c.addWordh("LEFT", 10, 3);
c.addWordv("SAFE", 8, 5);
System.out.print(c);
}
}
As you can see in Main method that i am adding the words but also giving the row and column number to place the words like c.addWordv("Safe",8,5); where 8 and 5 is column number.
Now Question is how can i implement cross word algorithm which just take words and place them on board randomly without taking the row and column numbers.
Thanks in advance
EDIT:
I want to modify this class algo the way that i dont have to give away the rows and columns number..
//Pseudo Code
If the crossword size is maxSize and any word's length is stored in wordLength ,then you can use random method as below
int maxSize=20;
int wordLength=4;
Random random =new Random();
int r,c;
//for horizontal
r=random.nextInt(maxSize-wordLength);
c=random.nextInt(maxSize);
//for vertical
r=random.nextInt(maxSize);
c=random.nextInt(maxSize-wordLength);
You can store the row and column and generate the new one if its already present.

Greedy LCS Java Code Implementation

I'm trying to make java code from this journal:
Journal Greedy LCS
This code is about a greedy approach for computing longest common subsequences by afroza begum.
And here is my [EDITED] code:
public class LCS_Greedy {
public static void main(String[] args) {
// String X_awal = "ABCBDABE";
// String Y = "BDCABA";
// String X_awal = "XMJYAUZ";
// String Y = "MZJAWXU";
// String X_awal ="AGGTAB";
// String Y="GXTXAYB";
// String X_awal = "ABCDGH";
// String Y = "AEDFHR";
// String X_awal = "AHBCDG";
// String Y = "AEDFHR";
//greedy not always optimum
String X_awal = "bcaaaade";
String Y = "deaaaabc";
String X = match(X_awal, Y);
int[] Penanda_Y = new int[Y.length()];
int y_length = Y.length();
for (int k = 0; k < y_length; k++) {
Penanda_Y[k] = 0;
}
int m = X.length();
int n = Y.length();
String L = "";
String LSym = "";
int R = 0;
int i = 1;
int[] P = new int[100];
P[i] = posisi(X, Y, i, Penanda_Y, R);
i = 1;
while (i <= m) {
if (i != m) {
P[i + 1] = posisi(X, Y, (i + 1), Penanda_Y, R);
}
if (P[i + 1] == 0) {
if (P[i] > R) {
L = L + " " + Integer.toString(P[i]);
LSym = LSym + " " + X.charAt(i - 1);
}
break;
}
if (P[i + 1] < R || P[i] < R) {
R = 0;
}
if (P[i] > P[i + 1]) {
if (R == 0 && i > 1) {
L = L + " " + Integer.toString(P[i]);
LSym = LSym + " " + X.charAt(i - 1);
Penanda_Y[P[i] - 1] = 1;
R = P[i];
i = i + 1;
if (R == Y.length() || i > X.length()) {
break;
}
P[i] = posisi(X, Y, i, Penanda_Y, R);
} else {
L = L + " " + Integer.toString(P[i + 1]);
LSym = LSym + " " + X.charAt(i + 1 - 1);
Penanda_Y[P[i + 1] - 1] = 1;
R = P[i + 1];
i = (i + 1) + 1;
if (R == Y.length() || i > X.length()) {
break;
}
P[i] = posisi(X, Y, i, Penanda_Y, R);
}
} else {
if (R == 0 && i > 1) {
L = L + " " + Integer.toString(P[i + 1]);
LSym = LSym + " " + X.charAt(i + 1 - 1);
Penanda_Y[P[i + 1] - 1] = 1;
R = P[i + 1];
i = (i + 1) + 1;
if (R == Y.length() || i > X.length()) {
break;
}
P[i] = posisi(X, Y, i, Penanda_Y, R);
} else {
L = L + " " + Integer.toString(P[i]);
LSym = LSym + " " + X.charAt(i - 1);
Penanda_Y[P[i] - 1] = 1;
R = P[i];
i = i + 1;
if (R == Y.length() || i > X.length()) {
break;
}
P[i] = posisi(X, Y, i, Penanda_Y, R);
}
}
}
System.out.println("X = " + X_awal);
System.out.println("X = " + Y);
System.out.println("L = " + L);
System.out.println("LSym = " + LSym);
System.out.println("Length = " + LSym.length() / 2);
}
public static String match(String X, String Y) {
String hasil = "";
for (int i = 0; i < X.length(); i++) {
for (int j = 0; j < Y.length(); j++) {
if (X.charAt(i) == Y.charAt(j)) {
hasil = hasil + X.charAt(i);
break;
}
}
}
return hasil;
}
public static int posisi(String X, String Y, int i, int[] Penanda_Y, int R) {
int n = Y.length();
int k;
int kr = 0;
i = i - 1;
for (k = 0; k < n; k++) {
if ((X.charAt(i) == Y.charAt(k)) && Penanda_Y[k] == 0) {
kr = k + 1;
break;
}
}
for (k = R; k < n; k++) {
if ((X.charAt(i) == Y.charAt(k)) && Penanda_Y[k] == 0) {
kr = k + 1;
break;
}
}
return kr;
}
}
But when I run that program, in some case it has true output, but in another case it false. What is wrong with that code? Can anybody explain to me? Thanks
Edit:
My code now can run perfectly, but it seem far away from pseudo code in that journal. Can anybody explain this? Why I cannot make code exactly from the journal? It always error when I make from pseudo code given in that journal. Thanks.
The algorithm will not lead to LCS. The greedy guess was wrong. It assumes the early match will guaranty to form LCS. Even though, the pseudocode takes the earlier matching of first two in X, which may skip a lot of common substring at the beginning of Y. The other simple test is change last 'E' to 'A' in X, the algorithm won't be able to find last matching 'A' for sure. The truth is an early match may lock down the position to forbidden longer match. In other word, we may need to skip some early match, so that we can found a longer match. Dynamic programming way is proved to be true mathematically, however, this algorithm didn't.

Addition of two very long integers

For practice, I am trying to add two very long integers by placing them in arrays and adding the corresponding elements in the arrays. However, when trying to add the carry over, I'm having problems (I.e., the carry over is the 1, that for instance, you add to the tens place when you do 199 + 199 = 398).
When doing 167 + 189 I get the right answer which is 356. However, for this very example though (199 + 199), I'm getting 288 instead of 398. My question is, why do I get an incorrect answer when I do 199 + 199, if the carry over works well when I do 167 + 189?
if (stringNumOneLength == stringNumTwoLength)
{ int answer;
int carryOver = 0;
int answerArray[] = new int[stringNumOneLength + 1];
for (int i = 1; i <= stringNumTwoLength; i = i + 1)
{
answer = Character.getNumericValue(stringNumOne.charAt(stringNumOneLength - i)) + Character.getNumericValue(stringNumTwo.charAt(stringNumTwoLength - i) + carryOver);
System.out.println(answer);
if (answer >= 10)
{
for (int j = 0; j <= 9; j = j + 1)
{
if (10 + j == answer)
{
carryOver = 1;
answer = j;
System.out.println("The carryover is " + carryOver + ".");
}
}
}
else
{
carryOver = 0;
}
answerArray[stringNumOneLength + 1 - i] = answer;
}
System.out.println(Arrays.toString(answerArray));
}
The output is the following:
[1, 9, 9]
[1, 9, 9]
18
The carryover is 1.
8
2
[0, 2, 8, 8]
You're adding the carry to the character rather than to its value:
... + Character.getNumericValue(stringNumTwo.charAt(stringNumTwoLength - i) + carryOver);
You want to move the right parent inside the +.
Note your for loop is unnecessary. This does the same thing:
if (answer >= 10)
{
answer -= 10;
carryOver = 1;
System.out.println("The carryover is 1.");
}
else ...
In case you're interested in an idiomatic solution:
public class Test {
public String add(String a, String b) {
StringBuilder r = new StringBuilder();
int carry = 0;
for (int ia = a.length() - 1, ib = b.length() - 1; ia >= 0 || ib >= 0; ia--, ib--) {
int aDigit = ia < 0 ? 0 : Character.getNumericValue(a.charAt(ia));
int bDigit = ib < 0 ? 0 : Character.getNumericValue(b.charAt(ib));
int sum = carry + aDigit + bDigit;
if (sum >= 10) {
sum -= 10;
carry = 1;
}
else {
carry = 0;
}
r.append(Character.forDigit(sum, 10));
}
if (carry > 0) {
r.append('1');
}
return r.reverse().toString();
}
public void run() {
System.out.println("789 + 89 = " + add("789", "89"));
System.out.println("12 + 128 = " + add("12", "128"));
System.out.println("999 + 999 = " + add("999", "999"));
}
public static void main(String[] args) {
new Test().run();
}
}
The parenthesis on this line:
answer = Character.getNumericValue(stringNumOne.charAt(stringNumOneLength - i)) + Character.getNumericValue(stringNumTwo.charAt(stringNumTwoLength - i) + carryOver);
are wrong. Notice how the + carryOver is within the call to Character.getNumericValue.

Categories

Resources