• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I am having a problem getting my code to correctly count

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember my question is tied to count
 
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79067
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic