简体   繁体   中英

put hyphen after the 1st, 4th, and 9th character in java

I'm stuck on these two blocks of code which I cannot find solution or figure out why it isn't working.

Please help me without changing method return type.

Thank you very much.

1st block: //the 10th digit should be either X or the remainder but output '?'.

public static char getCheckSum(String isbn) {
    int sum = 0;
    char charSum;

    for (int i = 0; i < isbn.length(); i++) {
        int[] num = new int[isbn.length()]; 
        num[i] = Character.getNumericValue(i) * (i+1);
        sum = sum + num[i];
    }

    int last = sum % 11;

    if (last == 10){
        charSum = 'X';
    } else {
        charSum = (char) last;
    }

    return charSum;
}

2nd block: // this one yields null no matter what I do. I'm lost on this one.

public static String formatISBNWithHyphens(String isbn) {
    // original isbn:  123456789
    // new isbn :      1-234-56789-X
    char isbn10 = getCheckSum(isbn);// this method get sum and put 10th char in isbn


    String isbn10Str = Character.toString(isbn10);
    char[] c = new char[isbn10Str.length()];
    String[] cStr = new String[isbn.length()];
    String isbnStr = null;

    for (int i = 0; i < isbn10Str.length(); i++){
        c[i] = isbn10Str.charAt(i);
        if (i == 0 || i == 3 || i == 8 ) {
            c[i] += '-';
         }
         cStr[i] = Character.toString(c[i]);
         isbnStr += cStr[i];
    }

    return isbnStr;
}

In your first case, the value of last can be from 0 - 9 (If not 10 ).

You are converting the last to char , which does an ASCII conversion.

If you look at the ASCII table , you will find that values 0 - 8 represent special characters which cannot be printed on most consoles (And 9 is a tab). That's why you are seeing a ? since the console doesn't know how to print the char.

In your second case, you are doing isbnStr += cStr[i] , when isbnStr is null, it effectively does isbnStr = "null" + cStr[i] . And most probably your cStr[i] is an invisible character. That's why you are only seeing null in the return string.

Note that you will mostly end up hard to debug problems if you are working with char when you can do with String .

public static String formatISBNWithHyphens(String isbn) {
    // original isbn:  123456789
    // new isbn :      1-234-56789-X
    char isbn10 = getCheckSum(isbn);// this method get sum and put 10th char in isbn


    String isbn10Str = isbn + Character.toString(isbn10); // Edit: You want the whole ISBN, not just the last char.
    String[] cStr = new String[isbn.length()];
    String isbnStr = ""; // EDIT: Initialize to empty string instead of null

    for (int i = 0; i < isbn10Str.length(); i++){
        cStr[i] = Character.toString(isbn10Str.charAt(i));
        if (i == 0 || i == 3 || i == 8 ) {
            cStr[i] += "-"; // Edit: Adding a char to another char will result in a different char. Not a concatenated string. So use Strings instead.
         }

         isbnStr += cStr[i];
    }
    return isbnStr;
}

Note: This will fix your second problem with minimal changes (though there are much better approaches to achieve the same result).

But your first problem of ? still remains. It depends on what you want to use as the last character in isbn. For one thing you could do charSum = (char) (last + 'A'); to bring it alpha range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM