简体   繁体   中英

java equivalent code to javascript's isNaN

I've found a simple & nice Base64 library in javascript and want to createa a java class from it (i know about existing java libs for this purpuse!, but am not interested). So here is a piece of javascript code using isNaN.. i need a java equivalent

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

And containing javascript function:

// private property
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
private static final Integer NaN = 0;

public static String encode(String input){
    StringBuffer output = new StringBuffer();
    int chr1 = 0, chr2 = 0, chr3 = 0;
    int enc1, enc2, enc3, enc4;
    int i = 0;


    input = _utf8_encode(input);

    while (i < input.length()) {
        //before: chr1 = Character.codePointAt(input, i++);
        if (i+1>=input.length()){
            chr1 = NaN;
            i++;
        } else {
            chr1 = Character.codePointAt(input, i++);
        }

        if (i+1>=input.length()){
            chr2 = NaN;
            i++;
        } else {
            chr2 = Character.codePointAt(input, i++);
        }

        if (i+1>=input.length()){
            chr3 = NaN;
            i++;
        } else {
            chr3 = Character.codePointAt(input, i++);
        }

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        //System.out.println(chr2+ " -> "+ String.valueOf(Character.toChars(chr2)) + "| "+String.valueOf(Character.toChars(chr2)).length());
        //System.out.println(chr3+ " -> "+ String.valueOf(Character.toChars(chr3)) + "| "+String.valueOf(Character.toChars(chr3)).length());

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output.append(_keyStr.charAt(enc1))
              .append(_keyStr.charAt(enc2))
              .append(_keyStr.charAt(enc3))
              .append(_keyStr.charAt(enc4));

    }

    return output.toString();
}

private static boolean isNaN(int charCodePoint){
    if(charCodePoint==NaN)
        return true;
    else
        return false;
}

My last attempt in java was using:

Character.isDigit((char)chr2).. not OK..

see isNaN(int charCodePoint) .. also not OK


And containing java equivalent function:

//String _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

public static String encode(String input){
    StringBuffer output = new StringBuffer();
    int chr1, chr2, chr3;
    int enc1, enc2, enc3, enc4;
    int i = 0;

    input = _utf8_encode(input);

    while (i < input.length()) {
        chr1 = Character.codePointAt(input, i++);
        chr2 = Character.codePointAt(input, i++);
        chr3 = Character.codePointAt(input, i++);

        enc1 = (chr1 >> 2);
        enc2 = (((chr1 & 3) << 4) | (chr2 >> 4));
        enc3 = (((chr2 & 15) << 2) | (chr3 >> 6));
        enc4 = (chr3 & 63);

        if (Character.isDigit((char)chr2)==false) {
            enc3 = enc4 = 64;
        } else if (Character.isDigit((char)chr3)==false) {
            enc4 = 64;
        }

        output.append(_keyStr.charAt(enc1))
              .append(_keyStr.charAt(enc2))
              .append(_keyStr.charAt(enc3))
              .append(_keyStr.charAt(enc4));

    }

    return output.toString();
}

What is a correct java equivalent to javascript's isNaN function?

There is no direct equivalent applicable to your case.

Before calling codePointAt() , you need to check that the index is within bounds. If it's not, that's the equivalent if your JavaScript code getting a NaN from charCodeAt() . This would, however, complicate the rest of the code considerably.

In a nutshell, the JavaScript code is written in a manner that does not lend itself to a nice and easy translation into Java.

Try the following to check for number or not.

try{
    Integer.parseInt("your char/value");
    System.out.println("Its a number.");
   }catch(Exception e){
    System.out.println("Not a number.");
   }

Maybe try this:

public static boolean isNaN(char chr){
    try{
        Integer.parseInt(new String(new char[]{chr}));
        return false;
    }catch (Exception e) {      
    }
    return true;
}

sorry, fixed

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