-1

input a number and convert it into binary, octal and hexadecimal using built in methods of integer class

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by javaAddict
0
Scanner br = new Scanner(System.in);
        int num = br.nextInt();
        Integer a = Integer.valueOf(num);
        System.out.println(a.toBinaryString(num));
        System.out.println(a.toOctalString(num));
        System.out.println(a.toHexString(num));

Edited by Nick Evan: Added code-tags

1

Scanner br = new Scanner(System.in);
int num = br.nextInt();
Integer a = Integer.valueOf(num);
System.out.println(a.toBinaryString(num));
System.out.println(a.toOctalString(num));
System.out.println(a.toHexString(num));

The toBinaryString and the other methods are static, so you don't need to create a new Integer object. What's the point of using the object created if you are going to pass as parameter the number used to create it?

0

The toBinaryString and the other methods are static, so you don't need to create a new Integer object. What's the point of using the object created if you are going to pass as parameter the number used to create it?

Thnks. So the code should be

Scanner br = new Scanner(System.in);
        int num = br.nextInt();
        System.out.println(Integer.toBinaryString(num));
        System.out.println(Integer.toOctalString(num));
        System.out.println(Integer.toHexString(num));
0

Thnks. So the code should be

Scanner br = new Scanner(System.in);
        int num = br.nextInt();
        System.out.println(Integer.toBinaryString(num));
        System.out.println(Integer.toOctalString(num));
        System.out.println(Integer.toHexString(num));

Yes it is much simpler

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.