2 Replies - 6178 Views - Last Post: 31 October 2009 - 08:28 PM Rate Topic: -----

#1 the_prisoner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-October 09

Conversion of binary to decimal and decimal to binary

Posted 31 October 2009 - 08:10 AM

I'm trying to write a program that should continually prompt the user to select a conversion type and then to enter either an integer or binary value. The program should then display the corresponding binary or integer value. For this assignment I'm not allowed to use either Integer.toBinaryString(int) or Integer.parseInt(string,int). I have the basic structure done, but I'm having problems with the two conversion types.
import java.util.*;

public class ConversionofBinarytoDecimalandDecimaltoBinary
{
	public static void main(String[] args)
	{
	
		Scanner keyboardin = new Scanner(System.in);
		
		
		int option;
		String binary;
	
		//REPEAT WHILE Option is not '3'
		do{
			//DISPLAY menu
			displayMenu();//call the method displayMenu
			
			System.out.print("Select conversion type: ");
			option = keyboardin.nextInt();
			
			
			switch(option)
			{
				case 1:System.out.println("Binary to Decimal");
						 System.out.print("Enter Valid Binary number: ");
						 binary = keyboardin.next();
						 
							break;
				case 2:System.out.println("Decimal to Binary");
						 System.out.println("Enter Decimal number:");
							break;
			
			
				case 3:System.out.println("you have finished your conversions");
							
							break;

				default:System.out.println("invalid menu option chosen");
							
							break;
			
			}//end switch
			
			}while(option!= 3);

	}//end main
	public static void displayMenu()
	{
		
		System.out.println("1. Binary to Decimal");
		System.out.println("2. Decimal to Binary");
		System.out.println("3. Quit");
	}//end method
}//end class



Binary to decimal
import java.util.*;


	public class BinaryToDecimal
	  {
		public static void main(String[] args)
		  {
			Scanner keyboard = new Scanner(System.in);
					
						//number input by user
			String binary; 

						
			String number;

						//value of binary number
			int binValue = 0;
 
			 
			
			
			System.out.print("Enter a valid binary number:");
			binary = keyboard.next();
		
			//find length of number 
			int length = binary.length();
			
			for (int counter = 0; counter<=length; counter++){

				//Calculations for each one in the binary number
				if (number = 1)
					 {
				binValue = binValue +(2^counter);

				
				}

				
				

				//re-prompt if the number contains anything other than ones and zeros (Invalid binary number)
				else{
					System.out.println(binary + " is not a valid binary number.");
					System.out.print("Enter a valid binary number:");
					binary = keyboard.next();
				}
			}
			
			System.out.println("The decimal value of " + binary + "is: " + binValue);
			
		}
	
	}



Decimal to binary
import java.util.*;

public class ConversionofDecimaltoBinary
{
	public static void main(String[] args)
	{
	
		Scanner keyboardin = new Scanner(System.in);
		
		int [] output = new int [32];
		
		int x;
		int digit;
		int base =2;
		int digitno =0;
		
		x = keyboardin.nextInt();
		
		while (x>0)
		{
			digit = x%base;
			output[digitno] = digit;
			
			digitno++;
			x = x%base;
			
			for(int i = digitno - 1; i>=0; i--);
			System.out.println(digit[i]);
		}
	}
}
		



I have been trying to do this over the last two days and seem to be making no progress and I'm not even sure if I'm even going about the conversion ways the right way, so any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Conversion of binary to decimal and decimal to binary

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Conversion of binary to decimal and decimal to binary

Posted 31 October 2009 - 03:52 PM

For your Binary to Decimal, I noticed you were using the Carat symbol (^) for your exponents. This symbol converts the 2 operands to binary, then performs the Exclusive Or operation on them. For exponents, use the Math.pow(base, exponent) method instead. Also, you don't need 3 separate classes for this. Instead, you can use 2 methods in the one class. For binary to decimal, I would use the following method:

public int binaryToDecimal(int binaryValue){
	int decimal = 0, temp = 0;
	String binaryRep = binaryValue + "";
	for(int i = binaryRep.length()-1; i > -1; i--){
		temp = Integer.parseInt(binaryRep.charAt(i) + ""); 
		decimal += temp * Math.pow(2, binaryRep.length()-1-i); 
		}
	return decimal;
}



If you can't use the parseInt() method with only 1 parameter, try using the Integer.valueOf(String) method instead. Or, you can write your own method to return 1 or 0 based on the value of the character.

For your decimal to binary problem, I'd use a few key elements. For one, consider using the decimal%2 operation to get the remainder and the decimal/2 operation to get the next number. Next, you may consider using String concatenation to store your values. If you only want to print out the decimal representation, then you can simply have this method return a String. Try implementing this method and feel free to post if you run into any more problems. Good luck!
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Conversion of binary to decimal and decimal to binary

Posted 31 October 2009 - 08:28 PM

a lots of main(String[] args) methods here we are kind of lost

Which one are you invoking when you start your application ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1