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

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 6 Years Ago by Nick Evan: Added code-tags

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?

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));

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 article has been dead for over six months. Start a new discussion instead.