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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Issue with Base Changing..

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default 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;
    				int answer1 = (last*1)+(next1*10)+(next2*100);
    				int answer2 = (next4*1000)+(next5 *10000)+(next6*100000)+(next7*1000000);
    				int answer3 = (next8*10000000)+(next9*100000000)+(first*1000000000);
    				int answer =  answer1 + answer2 + answer3;
    				System.out.println(x + " base 10 to base " + n + " _____");
    				System.out.println(answer1);
    				System.out.println(answer2);
    				System.out.println(answer3);
    				System.out.println(answer);
    				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);
     
     
    				int reply = scan.nextInt();
     
    				if(reply == answer){
    					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"
    Simplicity calls for Complexity. Think about it.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,319
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Issue with Base Changing..

    Can you show what is on the console for when the code works and when the code fails?
    Add comments to the output describing what is wrong and what you'd like the output to be.
    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. #3
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Issue with Base Changing..

    Quote Originally Posted by Norm View Post
    Can you show what is on the console for when the code works and when the code fails?
    Add comments to the output describing what is wrong and what you'd like the output to be.
    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 _____

    123 (answer1)
    3000 (answer2)
    0 (answer 3)
    3123 (answer)
    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..
    Simplicity calls for Complexity. Think about it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,319
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default 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:
    int reply = scan.nextInt();


    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. #5
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Issue with Base Changing..

    Quote Originally Posted by Norm View Post
    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:
    int reply = scan.nextInt();


    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 _____

    answer1 = 401
    answer2 = 0
    answer3 = 0

    answer = 401

    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.
    Simplicity calls for Complexity. Think about it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,319
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default 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. #7
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

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

Similar Threads

  1. data base
    By abcdmmkk in forum Member Introductions
    Replies: 1
    Last Post: March 23rd, 2011, 09:57 AM
  2. Base Conversion
    By Pingu in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 22nd, 2011, 03:15 PM
  3. BASE CONVERSION
    By bgwilf in forum Algorithms & Recursion
    Replies: 6
    Last Post: November 13th, 2010, 12:02 PM
  4. Doubts Over Base Class
    By rakesh86shankar in forum Object Oriented Programming
    Replies: 1
    Last Post: September 29th, 2010, 10:04 AM
  5. Simple Data Base.
    By Reese in forum The Cafe
    Replies: 14
    Last Post: August 2nd, 2010, 03:23 PM