Integer to Binary

i'm almost done

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 4471 Views - Last Post: 05 March 2009 - 08:57 PM Rate Topic: -----

#1 barsoum89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 20-February 09

Integer to Binary

Post icon  Posted 05 March 2009 - 05:16 PM

hey guys i'm having a problem and i know what it is but i dont know how to solve it. i've been doing this program for about to hours straight trying to find answers but i cant and i was wondering if anyone could help me out. we need to enter an integer and output it as a binary number using the do-while statement and the switch statement. i have it down but i dont know how to store numbers that are returned in a loop. here is the code

/**
 * @(#)BCD.java
 *
 *
 * @author 
 * @version 1.00 2009/3/5
 */

public class BCD {

public static void main (String args[]) {

	{
		 String output = "";
		 String xn = javax.swing.JOptionPane.showInputDialog("Enter Number:");
		
		 int len = xn.length();  
		 int pos = 0;
		 char a = xn.charAt(pos);
		
  do{
	  
  
	  a = xn.charAt(pos);
   	
	switch (a) 
		{
	case '0':
		output = "0000";
		break; 	
	case '1':
		output = "0001";
		break;
	case '2':
		output = "0010";
		break;
	case '3':
		output = "0011";
		break;
	case '4':
		output = "0100";
		break;
	case '5':
		output = "0101";
		break;	
	case '6':
		output = "0110";
		break;
	case '7':
		output = "0111";
		break;
	case '8':
		output = "1000";
		break;
	case '9':
		output = "1001";
		break;
	 
	}
  
   pos = 1 + pos;
	
  } while (pos<len);  
	
	javax.swing.JOptionPane.showMessageDialog(null, "Number entered: "+xn+
													"\n"+"Binary Encoded Decimal: "+output);
   }
   
	
	
  }
  
}



any help is greatly appreciated
thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Integer to Binary

#2 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Integer to Binary

Posted 05 March 2009 - 05:34 PM

You can always do:

start by putting output to ""

String output = "";

then in your switch:

output = output + "0010";

but this is the least of your problem:

If the number enter is in HEX your algoritm would be right.. you will need to check also for A, B, C, D, E, F
but if the number is enter in decimal your algorithm doesn not work

10 hexadecimal is effectively 00010000 in binary but dot 10 in decimal is not 00010000
Was This Post Helpful? 0
  • +
  • -

#3 barsoum89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 20-February 09

Re: Integer to Binary

Posted 05 March 2009 - 05:40 PM

View Postpbl, on 5 Mar, 2009 - 06:34 PM, said:

You can always do:

start by putting output to ""

String output = "";


i already stated string output as that.


all i need to do is enter a number, run it by the variables in the switch, and put them into binary encoded decimal format.

for example if i entered 12 it would display 0001 0010

does that make sense?
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Integer to Binary

Posted 05 March 2009 - 06:07 PM

View Postbarsoum89, on 5 Mar, 2009 - 06:40 PM, said:

for example if i entered 12 it would display 0001 0010

does that make sense?


No...because that's not 12 in binary.
Was This Post Helpful? 0
  • +
  • -

#5 barsoum89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 20-February 09

Re: Integer to Binary

Posted 05 March 2009 - 06:08 PM

View PostLocke, on 5 Mar, 2009 - 07:07 PM, said:

View Postbarsoum89, on 5 Mar, 2009 - 06:40 PM, said:

for example if i entered 12 it would display 0001 0010

does that make sense?


No...because that's not 12 in binary.


i mean like it takes each individual number and displays them as binary the 1is in binary and the 2 is in binary
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Integer to Binary

Posted 05 March 2009 - 06:11 PM

this is the weirdest decimal to binary algorithm i have ever seen :P
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Integer to Binary

Posted 05 March 2009 - 06:13 PM

Oh...like it displays the individual digits...that makes more sense.
Was This Post Helpful? 0
  • +
  • -

#8 barsoum89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 20-February 09

Re: Integer to Binary

Posted 05 March 2009 - 06:15 PM

View Postmostyfriedman, on 5 Mar, 2009 - 07:11 PM, said:

this is the weirdest decimal to binary algorithm i have ever seen :P


does it make sense though? we are supposed to extract each number from the string and display their binary for so 01234 would look like 0000 0001 0010 0011 0100
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Integer to Binary

Posted 05 March 2009 - 06:24 PM

it does make sense, however i wouldnt use a switch statement for it, i would write a method that computes the binary of a number by repeatedly dividing by 2, and in the main i can extract the each digit from the number using the %
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Integer to Binary

Posted 05 March 2009 - 06:24 PM

View Postbarsoum89, on 5 Mar, 2009 - 05:15 PM, said:

does it make sense though? we are supposed to extract each number from the string and display their binary for so 01234 would look like 0000 0001 0010 0011 0100


False

12 hex is 0001 0010
but
12 decimal is 1100

as MostyFriedman mentionned the "wierdest deimal to binary algorithm" :blink:

I guess you can:
- convert as you do the number from String to its decimal value
- use the Integer class toString(number, 16) method to translate it to hex
- do your switch on every character coming from that conversion (String method charAt(index))

you'll have to add A, B, C, D, E, F into your switch
Was This Post Helpful? 0
  • +
  • -

#11 barsoum89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 20-February 09

Re: Integer to Binary

Posted 05 March 2009 - 06:25 PM

View Postmostyfriedman, on 5 Mar, 2009 - 07:24 PM, said:

it does make sense, however i wouldnt use a switch statement for it, i would write a method that computes the binary of a number by repeatedly dividing by 2, and in the main i can extract the each digit from the number using the %


i have to use a switch statement. the statement is perfect how it is i just dont know the function on how to store the number the loop outputs
Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Integer to Binary

Posted 05 March 2009 - 06:35 PM

public class Test {

public static void main (String args[]) {

	{
		 String output = "";
		 String xn = javax.swing.JOptionPane.showInputDialog("Enter Number:");
	   
		 int len = xn.length();  
		 int pos = 0;
		 char a = xn.charAt(pos);
	   
  do{
	 
 
	  a = xn.charAt(pos);
	   
	switch (a)
		{
	case '0':
		output += "0000";
		break;	
	case '1':
		output += "0001";
		break;
	case '2':
		output += "0010";
		break;
	case '3':
		output += "0011";
		break;
	case '4':
		output += "0100";
		break;
	case '5':
		output += "0101";
		break;	
	case '6':
		output += "0110";
		break;
	case '7':
		output += "0111";
		break;
	case '8':
		output += "1000";
		break;
	case '9':
		output += "1001";
		break;
	 
	}
 
   pos = 1 + pos;
   
  } while(pos<len);  
   
	javax.swing.JOptionPane.showMessageDialog(null, "Number entered: "+xn+
													"\n"+"Binary Encoded Decimal: "+output);
   }
   
   
   
  }
 
}


you were SOO close..you just needed to concatenate the output of the previous binary in the loop to the current one
Was This Post Helpful? 0
  • +
  • -

#13 barsoum89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 20-February 09

Re: Integer to Binary

Posted 05 March 2009 - 06:50 PM

View Postmostyfriedman, on 5 Mar, 2009 - 07:35 PM, said:

you were SOO close..you just needed to concatenate the output of the previous binary in the loop to the current one


can you tell me how to do that?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Integer to Binary

Posted 05 March 2009 - 06:55 PM

Anxious to see the results...
let us know how 20 translate to binary following your algoritm :D

I'll give you a hint: the answer is: 0001 0100
Was This Post Helpful? 0
  • +
  • -

#15 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Integer to Binary

Posted 05 March 2009 - 06:56 PM

output += "0000";



suppose i enter 0, then the above line will concatenate the string 0000 to "" (which is the string that you initialized output to be), and now the new value of output will be 0000, on the second iteration suppose the character is 2, then it will concatenate output with 0001 and the new output will be 00000001 and so on and so forth

output += "0000";
is the same as
output = output + "0000";


Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3