So, i need to convert a decimal number to a given base. Pls halp
o
I don't have the program for this extension... :x
:/ how do
ima post my code and see if you could maybe help
Ok
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; }
What language is this script in?
java
Hold on
Sorry I was looking at it a bit.
I don't see what's wrong here... :l What errors are you getting here?
I don't see what's wrong here... :l What errors are you getting here?
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
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() ```
Join our real-time social learning platform and learn together with your friends!