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

Recommended Answers

All 4 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.