3

I'm trying to add 13 individual digits from a string together. I thought using a while loop would be the best way to do this, but I think I messed something up. Here's my code:

import java.util.Scanner;

public class ISBNChecker{
    public static void main(String [] args){
        String isbnNumber;
        int isbnTotal= 0;
        int index = 0;

        Scanner scnr = new Scanner(System.in);
        System.out.println("Enter a 13 digit ISBN Number:");
        isbnNumber = scnr.nextLine();
        if (isbnNumber.length() != 13) {
            System.out.println("Error- 13 numerical digits required");
        }
        char num = isbnNumber.charAt(index);
        while (index <13) {
        if (index % 2 == 0) {
            isbnTotal = isbnTotal + num;
            index =index + 1;
        }
        else { 
            isbnTotal = isbnTotal + (3 * num);
            index = index + 1;
        }
        }
        System.out.println(isbnTotal);
        if (isbnTotal % 10 == 0) {
            System.out.println("Valid ISBN Number");
        }
        else {
            System.out.println("Invalid ISBN Number");
        }
    }
}

I'm using the input 9780306406157, which should be an invalid ISBN Number. The value of isbnTotal at the end of the program should be 100, but instead, it is 1425. Any help in figuring out how to fix it would be appreciated. Also, the formula I'm using for the problem is x1 + 3x2 + x3 + 3x4 ... +x 13 for reference!

1
  • Consider thanking others by accepting an answer in your questions, and maybe an upvote. Commented Jun 21, 2020 at 14:03

3 Answers 3

5

I found your bug, you made some mistake.

int index = 0;
while (index < 13) {
    char num = (char) (isbnNumber.charAt(index) - '0');
    
    if (index % 2 == 0) {
        isbnTotal = isbnTotal + num;
    } else {
        isbnTotal = isbnTotal + (3 * num);
    }
    
    index++;
}

Problem #1

The code

 char num = isbnNumber.charAt(index);

Was not in your while loop, causing your code to always run with the same character.

Problem #2

When doing

char num = isbnNumber.charAt(index);

You are actually getting the ASCII value of the character. What you cant to get is the value of the number right ? So you have to do:

char num = (char) (isbnNumber.charAt(index) - '0');

Notice that the zero is between two single quote, that because we want the value of the ZERO ASCII CHARACTER (which is 38).

'1' - '0' = 1

'9' - '0' = 9

EDIT: I forgot to mention that you should check before if the character is a number, else you will maybe try to do something like 'A' - '0' which will be equal to 17

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a detailed working code for your ISBN checker

import java.util.Scanner;

public class ISBNChecker{
    public static void main(String [] args){
        String isbnNumber;
        int isbnTotal= 0;
        int index = 0;

        Scanner scnr = new Scanner(System.in);
        System.out.println("Enter a 13 digit ISBN Number:");
        isbnNumber = scnr.nextLine();
        if (isbnNumber.length() != 13) {
            System.out.println("Error- 13 numerical digits required");
        }
        //Perform other operations if it's length is 13
                else{
        //initiliizing num for 1st time + convert the character at index to 
        number
        int num = Character.getNumericValue(isbnNumber.charAt(index));
        while (index <13) {
        if (index % 2 == 0) isbnTotal = isbnTotal + num;
        else isbnTotal = isbnTotal + (3 * num);
    
        //increment outside of if else - less code
        index = index + 1;
        //verify if index is not out of bounds for charAt() 
        //then change num value + conversion
        if(index<13 )
        num = Character.getNumericValue(isbnNumber.charAt(index));
    
        }
          
        System.out.println(isbnTotal);
        if (isbnTotal % 10 == 0)   System.out.println("Valid ISBN Number");
        else  System.out.println("Invalid ISBN Number");
        }
        

    }
}

As mentioned in the answer below, nextLine() gets input in ASCII characters and it is important to convert that into numbers when there are numerical calculations involved.

Comments

0

Well, it seems that you do allow string inputs longer or shorter than 13.

Then you should return after this line:-

System.out.println("Error- 13 numerical digits required");  

Otherwise, the code will run even if it's not at the wanted length

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.