Java Weird Output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Java Weird Output

Hi! I am new to Java and am trying to go through a couple USACO problems, but I am getting weird output that I don't get in Python. In this program, I am trying to get the product of all the characters in a string (that I turned into a character array) where 1 represents "A", 2 represents "B", .... For some reason, when the groupnum variable should be equal to 17955, my program is outputting a random large negative number. I know that since the answer is under 2 billion, the problem is not that the result is too large for the int type. Thanks for your help! https://code.sololearn.com/cgSZlCFg9ZNE/#java

11th Aug 2020, 1:50 PM
Connor
Connor - avatar
3 Answers
+ 5
In the for-each loop, why you decrement <c> by 96? considering all capital letters, shouldn't it be c - 64?
11th Aug 2020, 1:59 PM
Ipang
+ 1
Wow that fixed it. Thank you so much! Ascii values have always been confusing for me.
11th Aug 2020, 2:01 PM
Connor
Connor - avatar
+ 1
Glad it helps! 👌
11th Aug 2020, 2:02 PM
Ipang