what do i need to change if i want this program to convert decimal to hexadecimal thanks.

package dectooctal;

import java.util.Scanner;
public class Dectooctal {


    public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       int broj;

       System.out.print("ENTER NUMBER THAT YOU WANT TO CONVERT TO OCTAL: ");
       broj = input.nextInt();
       convert(broj);
    }
    public static void convert (int a){
        String obrnuti =" ";
        while (a>0){
            int x = a%8;
            obrnuti += x;
            a/=8;
        }
        System.out.println(reverse(obrnuti));
    }
    public static String reverse(String a){
        String novi =" ";
        for (int i=a.length()-1;i>=0; i--){
        char c = a.charAt(i);
        novi += c;
    }
        return novi;



        }


    }

Dani AI

Generated

There are three small but important fixes that will make your program correct, robust and easy to reuse. ’s original code works in principle but creates leading spaces, does repeated String concatenation (slow), never handles zero or negative inputs cleanly, and prints from the converter instead of returning a value. is right that library helpers exist, and ’s lookup idea is fine for a manual solution — below is a compact, safer manual implementation you can drop into your program.

Avoiding the original pitfalls:

  • don’t initialize accumulator strings with a space (it produces a leading space in output);
  • use StringBuilder and its reverse() to avoid O(n^2) string building;
  • handle 0 explicitly and preserve sign (watch out for Integer.MIN_VALUE);
  • validate input and separate conversion from printing so the method is reusable.

A compact, reusable converter that addresses those points:

public static String toBaseString(int number, int base) {
    if (base < 2 || base > Character.MAX_RADIX)
        throw new IllegalArgumentException("base must be 2.." + Character.MAX_RADIX);

    if (number == 0) return "0";

    boolean negative = number < 0;
    long n = number;
    if (n < 0) n = -n;           // handle Integer.MIN_VALUE safely
    StringBuilder sb = new StringBuilder();

    while (n > 0) {
        int digit = (int)(n % base);
        sb.append(Character.forDigit(digit, base));
        n /= base;
    }

    if (negative) sb.append('-');
    return sb.reverse().toString().toUpperCase();
}

Use toBaseString(value, 16) when you need hexadecimal. Also validate scanner input (check hasNextInt() or catch NumberFormatException) and rename local variables (e.g., number instead of broj) for clarity. This keeps logic testable and avoids the common bugs seen in the thread.

Recommended Answers

All 2 Replies

you're making it way too difficult.
check: this, this and this, for instance.

You need to divide and modulo by 16 instead of 8, and you need to use the letters 'A' through 'F' when x is greater than 9. A fun trick is with a table lookup of digits:

public static void convert(int a){
    String digits = "0123456789ABCDEF";
    String obrnuti = " ";

    while (a > 0){
        int x = a % 16;
        obrnuti += digits.charAt(x);
        a /= 16;
    }

    System.out.println(reverse(obrnuti));
}

you're making it way too difficult.

That's all well and good, but it's not unreasonable to assume that a teacher is disallowing use of the standard library for this exercise.

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.