Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (shadeyghost):

So, i need to convert a decimal number to a given base. Pls halp

OpenStudy (shadeyghost):

o

OpenStudy (anonymous):

I don't have the program for this extension... :x

OpenStudy (shadeyghost):

:/ how do

OpenStudy (shadeyghost):

ima post my code and see if you could maybe help

OpenStudy (anonymous):

Ok

OpenStudy (shadeyghost):

public static String makeBase(int x) { Scanner rdIn = new Scanner(System.in); System.out.println(" Please enter a base you would like to use: "); int n = rdIn.nextInt(); char[] z = null; for ( int a = 0; x != 0; a++) { z[a] = x % n; x %= n; } valueOf(z); return z; }

OpenStudy (anonymous):

What language is this script in?

OpenStudy (shadeyghost):

java

OpenStudy (anonymous):

Hold on

OpenStudy (anonymous):

Sorry I was looking at it a bit.

OpenStudy (anonymous):

I don't see what's wrong here... :l What errors are you getting here?

OpenStudy (anonymous):

I don't see what's wrong here... :l What errors are you getting here?

OpenStudy (rsmith6559):

This problem is recursive. Just keep taking the mod (remainder) of the decimal number divided by the base, and when you get the decimal number to zero, unwind it concatenating the mods. Depending on the base, you may need to set up an array of chars that the mod can be used to index into to get a value for the concatenation. Something like: characters = { "0123456789ABCDEF" }; // characters for hexadecimal

OpenStudy (woodrow73):

on a java syntax note, doing ``` char[] z = null; z[0] = 'a'; ``` will result in a NullPointerException being thrown. you could initialize it like this ``` int numSlotsInArray = 1; char[] z = new char[numSlotsInArray]; z[0] = 'a'; //keep in mind accessing slots out of bounds will throw an error z[1] = 'b'; //this line will throw ArrayIndexOutOfBoundsException ``` if you need a variable amount of slots, using ArrayList is an easy solution ``` ArrayList<Character> z = new ArrayList<Character>(); z.add('a'); z.add('b'); for(int i = 99; i <= 122; i++) { z.add((char)i); //looping through alphabet in ascii } //print out list of lower case alphabet for(int i = 0; i < z.size(); i++) { System.out.println(z.get(i)); } //other helpful methods of arraylist are .set(), .remove() ```

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!