Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Need to make a method for taking an input, num= stdIn.nextLine(); and converting it into a binary string. "public static String toBinary(String num)". In Java. Any ideas? Can only find for int to binary. Needs to be string so user can enter "q" to exit program.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
156 views
Welcome To Ask or Share your Answers For Others

1 Answer

public static String toBinary(String num) {
    int convertedNum = Integer.parseInt(num);
    return Integer.toBinaryString(convertedNum);
}

Note that you should catch possible runtime exceptions because of the integer conversion of the String value.

I hope I helped you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...