-6

I'm writing a game which makes the user guess the number that is generated randomly. In the end it would show the total number correct and the sum of those correct numbers. However, I cannot show the right sum of the correct number that the user has. Can someone help me with this? Thank you!

public static void main(String[] args) {
    Random rng = new Random();
    Scanner consoleScanner = new Scanner(System.in);
    String inputString;
    int answer = rng.nextInt(90000) + 10000, sum, numberCorrect;
    System.out.print("I have randomly chosen a 5-digit code for you to guess.\n"
            + "Each time you guess, I will tell you how many digits are correct and the sum of the digits that are correct.\n"
            + "For example, if the number is \"68420\" and you guess 12468, I will respond:\n"
            + "Number of Digits Correct: 1\n" + "Sum of Digits Correct   : 4\n"
            + "From deduction, you will know the 4 was correct in the guess.\n\n"
            + "Now its your turn..................................................................\n" + "answer = "
            + answer);
    do {
        System.out.print("\nPlease enter a 5-digit code (your guess): ");
        inputString = consoleScanner.nextLine();
        numberCorrect = 0;
        sum = 0;
        if (inputString.length() != 5) {
            System.out.println("Please enter 5-digit code only.");
            System.exit(0);
        }
        for (int i = 0; i < 5; i++) {
            String answerString = String.valueOf(answer);
            if (inputString.charAt(i) == answerString.charAt(i)) {
                numberCorrect++;
                char digit = answerString.charAt(i);
                sum += digit;
            }
        }
        System.out.println("Number of Digits Correct: " + numberCorrect + "\nSum of Digits Correct:    " + sum);
    }
    while (numberCorrect < 5);
    System.out.println("****HOORAY!  You solved it.  You are so smart****");
}
Jason Braucht
  • 2,358
  • 19
  • 31
Hieu Pham
  • 1
  • 2
  • 10
    Sorry, this is not how StackOverflow works. Questions of the form _"Here's a bunch of my code, please debug it for me"_ are considered off-topic. Please visit the [help] and read [ask] for more information, and especially read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236/18157) – Jim Garrison Mar 08 '17 at 20:29
  • 1
    Your digit variable is type char. So when we get a '0' its actually the integer value of 48 (ASCII table) you need to convert the char to an int. – TheQAGuy Mar 08 '17 at 20:30

2 Answers2

0

Your sum variable is integer and you are trying to add it to char (digit). you need to convert the character to valid int and then add it to sum.

following references may help!

Java: parse int value from a char

or

Converting characters to integers in Java

Community
  • 1
  • 1
01000001
  • 833
  • 3
  • 8
  • 21
-1

You have to convert your char into a numeric value:

sum += Character.getNumericValue(digit);
Semaphor
  • 900
  • 2
  • 12
  • 33