Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Issue with Base Changing..

1. ## Issue with Base Changing..

Focus on the code inside the While Loop

```System.out.println("\nThis program is built to enhance your Base Changing skills");
System.out.println("Change the Base 10 number to the Base n number");
System.out.println("Enter the number of equations to work on below:");

int amount = scan.nextInt();
int a = 0;
int correct = 0;
int wrong = 0;
int score = 0;
double start = System.currentTimeMillis();

while(a < amount){
int x = 100 + (int)(Math.random()*899);
int n = 2 + (int)(Math.random()*7);
int last = x % n;
int next1 = (int)((x/n))%n;
int next2 = (int)((x/(n*n)))%n;
int next3 = (int)((x/(n*n*n)))%n;
int next4 = (int)((x/(n*n*n*n)))%n;
int next5 = (int)((x/(n*n*n*n*n)))%n;
int next6 = (int)((x/(n*n*n*n*n*n)))%n;
int next7 = (int)((x/(n*n*n*n*n*n*n)))%n;
int next8 = (int)((x/(n*n*n*n*n*n*n*n)))%n;
int next9 = (int)((x/(n*n*n*n*n*n*n*n*n)))%n;
int first = (int)((x/(n*n*n*n*n*n*n*n*n*n)))%n;
System.out.println(x + " base 10 to base " + n + " _____");
System.out.println(last);
System.out.println(next1);
System.out.println(next2);
System.out.println(next3);
System.out.println(next4);
System.out.println(next5);
System.out.println(next6);
System.out.println(next7);
System.out.println(next8);
System.out.println(next9);
System.out.println(first);

System.out.println("Correct!");
correct++;
score += 5;
}
else{
System.out.println("Wrong!");
wrong++;
score -= 9;
}
amount--;
}

double now = System.currentTimeMillis();
double time = (now-start)/1000;

System.out.println("\nSTATISTICS:");
System.out.println("Correct: " + correct);
System.out.println("Wrong: " + wrong);
System.out.println("Score: " + score);
System.out.println("Time taken: " + time + " seconds");
System.out.println("Time per Question: " + time/(wrong + correct) + " seconds");```

I've made this (admittedly poorly done) algorithm to change from Base 10 to Base n. However, once I get around the 1,000's place. Some issues begin to arise. I printed out all the values for the variables... say I get:

last = 1
next1 = 2
next2 = 2
next3 = 1

The answer will give 221, instead of 1221. Any help please? (Also, sometimes it works perfectly fine. Usually anything past 3 digits will result in a "false" answer"

2. ## Re: Issue with Base Changing..

Can you show what is on the console for when the code works and when the code fails?
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

3. ## Re: Issue with Base Changing..

Originally Posted by Norm
Can you show what is on the console for when the code works and when the code fails?
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.
This program is built to enhance your Base Changing skills
Change the Base 10 number to the Base n number
Enter the number of equations to work on below:

1

859 base 10 to base 4 _____

3 (ones place)
2 (tens place)
1 (hundreds place)
1 (thousands place)
3 (ten thousands place)
0 (hundred thousands place)
0 (etc.. etc.. etc..)
0
0
0
0

The answer should be 31123, not 3123..

4. ## Re: Issue with Base Changing..

What code produces the output you have posted? I don't see the any of the (...) Strings in your code?
You have over a dozen println statements that print numbers without and ids on them.
Can you change them like this to show what is being printed:
System.out.println("last=" + last);

I don't understand what the user is to enter here:

Can you explain your algorithm for generating a String representation for a given integer value?
For example for the value ten: "10" base 10, "11" base 9, "12" base 8, ... or "1010" base 2

5. ## Re: Issue with Base Changing..

Originally Posted by Norm
What code produces the output you have posted? I don't see the any of the (...) Strings in your code?
You have over a dozen println statements that print numbers without and ids on them.
Can you change them like this to show what is being printed:
System.out.println("last=" + last);

I don't understand what the user is to enter here:

Can you explain your algorithm for generating a String representation for a given integer value?
For example for the value ten: "10" base 10, "11" base 9, "12" base 8, ... or "1010" base 2

This program is built to enhance your Base Changing skills
Change the Base 10 number to the Base n number
Enter the number of equations to work on below:

1

226 base 10 to base 5 _____

last = 1
next1 = 0
next2 = 4
next3 = 1
next4 = 0
next5 = 0
next6 = 0
next7 = 0
next8 = 0
next9 = 0
first = 0

This one's answer should be 1401...

For int reply = scan.nextInt(); ...... This takes the user's answer as an integer called reply (It works, don't question it please. I have several other functions that work fine)

For my algorithm... I have an integer x that has a range from 100-999. I have another integer n that ranges from 2-9. x = the Base 10 value. And n is the "Base n" to convert to. The way my algorithm works is so:

In order to change from Base 10 to a specific Base n.. You must do these steps:

Say we have "132 Base 10", and we wish to convert it to Base 4...

We must divide 132 by 4 which comes out to be 33 remainder 0. We record the 0. This 0 will be the ones place.
We then take the remaining integer 33, and divide that by 4. We get 8 remainder 1. Record the 1 for tens place.
Then take the 8 and divide by 4. We get 2 remainder 0. Record the 0 for the hundreds place.
Finally take the 2 and divide it by 4. We got 0 remainder 2. Record the 2 for the thousands place.

Overall, our answer should be 2010 Base 4. That's how the conversion works. In order to work for all bases 2-9.. I had to make 10 different place values (last, next1, next2, next3, next4, ..., first). You can see that I made every place value by doing so... last*1, next1*10, next2*100, next3*1000... And I JUST FOUND MY MISTAKE. Incredible. My issue lied with not including "next3 * 1000 within my algorithm. I left it out. Thanks for being around once again to help Norm.

6. ## Re: Issue with Base Changing..

Your algorithm description sounds like what you would do in a loop:
Dividing by a number and concatenating the remainder to a String to build the results.
Continue until all divisions are done.

Your current code is a mess.

7. ## Re: Issue with Base Changing..

What Norm is saying is you need to do something on the lines of
```int remainder;
long next;
String result = "", newLine = System.getProperty("line.separator"); //Not sure if you need this...
while(next > 0) //I don't know o.O
{
remainder = next%n;
next = next/n;
result = remainder+result; //Put the remainder in front of the String
}
System.out.println(result);```