• Post Reply Bookmark Topic Watch Topic
  • New Topic

I am having a problem getting my code to correctly count  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output is :
912
471


THe program is supposed to simulate the way in which numbers are taught to be added in school.

For example

12
+
9
2 + 9 = 11 so results equal 22

I cannot get 1 to add to the middle 7 integer in the output for the life of me.
I fell that the problem lies within its placement in the code or maybe it is being reset at the end of each loop.
Hopefully this question is not to obnoxious.





 
Nicholas Seamans
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember my question is tied to count
 
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are adding "262" and "912". When you add last digits 2 and 2, it equals to 4 (which is less than 10). So why do you expect a carry-over of 1(from adding 2 and 2) when you add the second digits 6 and 1? You might try numbers "269" and "912" in that case.

And your code will not compile because of line 34 count = ;
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to do arithmetic on Strings? They represent writing, not numbers.
Did you know that you can divide a String into an array of chars?
Did you know that chars are numbers (not letters), and you can do arithmetic with them? So you can use your char array to create an int array.
What is going to happen when you get a 4‑digit number out of two 3‑digit numbers? Will you have enough space in your array?

you should move all that code out of the main method other methods, and you should think of better names for your arrays. There are much better ways to get the carry than using an if.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!