• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

summing elements of array and getting strange behavior

 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Ok, this isn't a school assignment...just a random exercise I found on the internet since I'm beginning to learn a bit about arrays. For my version, user enters the number of indexes the array will have, and then enters data for the elements, and we output the sum. The sort of tricky part (part of the exercise I suppose is thinking about primitive variables) is that the input for elements should be a char representing a single integer and output should be in terms of a long. Again, not really sure why because I would not go about the task this way...but I want to complete the exercise per instructions as a learning experience.

The program below compiles fine. However, let's say I chose 1 index for my array, and enter 1 as my element..the program returns 49. If I chose 1 index and enter 2 as my element, it returns 50...and so on according to pattern. My question is first can you point me in the direction of where I'm going wrong? Second, I guess this is more theoretical, any ideas for why it is beginning at 49?

Here is my code:



Thanks in advance for your help
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's giving the Ascii char value.

http://www.asciitable.com/

 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome Aj, thanks!

Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic