Results 1 to 3 of 3
  1. #1
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Help with converting to base 10 program

    I am trying to convert these into a base 10
    1010
    1000
    1001
    0101
    0110
    1100
    1110
    1111
    11111
    111111
    1110101
    10101010101
    1010101010110
    111111111111111111
    Here is my code
    Java Code:
    	import java.io.*;
    import java.util.*;
    	public class prog606 {
    		public static void main (String [] args)throws IOException
    				{
    			Scanner file = new Scanner(new File("prog606.dat"));
    			int size = file.nextInt();
    			
    			while(file.hasNextInt())
    	        {
    	            int num = file.nextInt();
    	            if (num<0) num=-num; // maybe you'd like to support negatives
    	            List<Integer> digits = new LinkedList<Integer>();
    
    	            while (num>0) {
    	                digits.add(0, num%10);
    	                num=num/10;
    	            }
    	            	int bobby = getbase(digits);
    	            System.out.println("base is "+bobby);            
    	        }
    	        }
    	public static int getbase(List<Integer> x)
    					{	
    						int base = 0;
    						int sum = 0;
    						Collections.reverse(x);
    						for(int i = 0; i<x.size();i++){
    							int jay = x.get(i);
    							for(int k = 0;k<x.size();k++){
    								sum = jay*(2^k);
    								base+=sum;
    							}	
    						}
    							return base;  
    						}
    					}
    Here is my output:
    Java Code:
    base is 12
    base is 6
    base is 12
    base is 10
    base is 10
    base is 12
    base is 18
    base is 24
    base is 60
    base is 114
    base is 115
    The bases are supposed to be

    1010 == 10

    1000 == 8

    1001 == 9

    0101 == 5

    0110 == 6

    1100 == 12

    1110 == 14

    1111 == 15

    11111 == 31

    111111 == 63

    1110101 == 117

    10101010101 == 1365

    1010101010110 == 5462

    111111111111111111 == 262143

    I can't find what I'm doing wrong

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Help with converting to base 10 program

    Just some suggestions without knowing the exact reason right now:
    - negative numbers (EDIT: _binary_ numbers) are usually not signed by a minus. That means your input cannot be negative.
    - why don't you just use the input string and go through its characters ('0' and '1' are much more easily to be identified and debugged)
    - what do you need that inner sum loop for? You may use the leftshift operator "<<" like "jay << k" which is "jay" leftshift by k bits.
    - you could as well use the "BigInteger" class for your conversion - as in BigInteger("100101000111111110000", 2);
    Last edited by Sierra; 04-23-2012 at 01:24 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Help with converting to base 10 program

    Try debugging the code by printing out the values of the variables as the code executes to see what it is doing.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Converting Number to Base 8
    By nighteagle2 in forum New To Java
    Replies: 9
    Last Post: 02-17-2012, 02:38 AM
  2. Replies: 2
    Last Post: 11-27-2011, 05:11 PM
  3. Converting Program from ArrayLists to Arrays
    By Java-Guy in forum New To Java
    Replies: 3
    Last Post: 01-26-2011, 09:02 AM
  4. Converting an array program to ArrayList
    By Adomini in forum New To Java
    Replies: 4
    Last Post: 10-13-2010, 07:22 PM
  5. A Number Converting Program!
    By WastedxYears in forum New To Java
    Replies: 2
    Last Post: 01-09-2010, 12:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •