binary number progblem

binary number progblem

Page 1 of 1

10 Replies - 1449 Views - Last Post: 02 October 2007 - 07:08 AM Rate Topic: -----

#1 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

binary number progblem

Post icon  Posted 30 September 2007 - 10:39 AM

A guys I have a problem when I am trying to flip the numbers between 0 and 1. It's in my switch loop. It doesn't do it at all.

Ex: 1001->0110.
It should work like this but doesn't . Please help me.
package Bynary;
import java.util.*;
import java.io.*;

public class Bynary
{
		
	   
				   public static void main(String[] args)throws IOException
				   {
					   InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.println("What do you want the program to do?");
System.out.println(" Decimal to binary ... 1");
System.out.println(" Binary to decimal ... 2");
String choice = input.readLine();
int c = Integer.parseInt(choice);
 
if(c==1)
{
System.out.println(" Enter a decimal number:");
String dba=input.readLine();
int db = Integer.parseInt(dba);
System.out.println(db);
int [] m = new int [10];
int j;
for(j=8;j>=1;j--)
{
m[j] = db%2;
db= db/2;
}
int sc = 0;
for(j=1;j<=8;j++)
{
if(m[j]==0)
{
if(sc==0)
{
continue;
}
}
System.out.print(+m[j] + " ");
sc = sc+1;
}
}
 
if(c==2)
{
System.out.println("Enter a binary number");
String bda=input.readLine();
int l=bda.length()-1;
double sum =0;
for (int i = 0; i <= bda.length(); i++)
{
switch(i)
   {
	  case '0': bda =  bda + "0";
	  break;
	  case '1': bda = bda + "1";
	  break;
	  default:
	  System.out.println("Thanks");
  }
}
for(int w =0;w<bda.length();w++)
{
char r = bda.charAt(w);
String str = new Character®.toString();
int er = Integer.parseInt(str);
sum = sum + (er*Math.pow(2,l));
l--;
}
System.out.println("The decimal is : "+sum);

						  
						   System.out.println("The decimal is : "+sum);
						   System.out.println("-" + sum);
						   input.readLine();
						   
					   }
				   }
}


Is This A Good Question/Topic? 0
  • +

Replies To: binary number progblem

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: binary number progblem

Posted 30 September 2007 - 10:50 AM

OK first this code is for bynary to decimal and vise versa(sort of)

My suggestion to do 1001->0110 is, this little binary arithmetic thing:
1111-1001=0110
Was This Post Helpful? 0
  • +
  • -

#3 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

Re: binary number progblem

Posted 30 September 2007 - 10:54 AM

View PostPennyBoki, on 30 Sep, 2007 - 10:50 AM, said:

OK first this code is for bynary to decimal and vise versa(sort of)

My suggestion to do 1001->0110 is, this little binary arithmetic thing:
1111-1001=0110



The point is it can be any number. I just use one of the them, to show the example. Please you have another idea how can i fix my program.
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: binary number progblem

Posted 30 September 2007 - 11:04 AM

View Postjack85, on 30 Sep, 2007 - 10:54 AM, said:

View PostPennyBoki, on 30 Sep, 2007 - 10:50 AM, said:

OK first this code is for bynary to decimal and vise versa(sort of)

My suggestion to do 1001->0110 is, this little binary arithmetic thing:
1111-1001=0110



The point is it can be any number. I just use one of the them, to show the example. Please you have another idea how can i fix my program.

I know that. I give also an example based on your example. So you need to know the number of bits of the number that the user enters. Another way to do this is with string manipulation, take the input of the user as a string then just replace the 0s with 1s and the 1s with 0s.
Was This Post Helpful? 0
  • +
  • -

#5 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

Re: binary number progblem

Posted 30 September 2007 - 11:09 AM

View PostPennyBoki, on 30 Sep, 2007 - 11:04 AM, said:

View Postjack85, on 30 Sep, 2007 - 10:54 AM, said:

View PostPennyBoki, on 30 Sep, 2007 - 10:50 AM, said:

OK first this code is for bynary to decimal and vise versa(sort of)

My suggestion to do 1001->0110 is, this little binary arithmetic thing:
1111-1001=0110



The point is it can be any number. I just use one of the them, to show the example. Please you have another idea how can i fix my program.

I know that. I give also an example based on your example. So you need to know the number of bits of the number that the user enters. Another way to do this is with string manipulation, take the input of the user as a string then just replace the 0s with 1s and the 1s with 0s.



That what I am trying to do with my swtich loop, but it doesn't working. If ti si posible can you show me a example that how can I fix my switch loop that it can work.
Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: binary number progblem

Posted 30 September 2007 - 11:44 AM

ok here is an example:

public class Example
{
	public static void main(String [] args)
	{
		String aaa="0100010101";//some input string
			
		char[] array;// some array of chars
		
		String tmp = new String();//tmp = temporary string 
		tmp=aaa;//give tha value of aaa to tmp
		array=tmp.toCharArray();// fill the array with the chars of the tmp string
		for(int i=0; i<aaa.length();i++)// loop through the array
		{	
			if(array[i]=='1')//if the char at that poistion is 1
			{
				array[i]='0';	//make it 0
			}
			else array[i]='1';	//if not make it 1
			
			
			
		}//end the for loop
 		aaa = new String(array);//make the array string
 		
	
		
		System.out.println(aaa);//print the complement of the new string 
	}
}

Was This Post Helpful? 0
  • +
  • -

#7 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

Re: binary number progblem

Posted 30 September 2007 - 12:03 PM

View PostPennyBoki, on 30 Sep, 2007 - 11:44 AM, said:

ok here is an example:

public class Example
{
	public static void main(String [] args)
	{
		String aaa="0100010101";//some input string
			
		char[] array;// some array of chars
		
		String tmp = new String();//tmp = temporary string 
		tmp=aaa;//give tha value of aaa to tmp
		array=tmp.toCharArray();// fill the array with the chars of the tmp string
		for(int i=0; i<aaa.length();i++)// loop through the array
		{	
			if(array[i]=='1')//if the char at that poistion is 1
			{
				array[i]='0';	//make it 0
			}
			else array[i]='1';	//if not make it 1
			
			
			
		}//end the for loop
 		aaa = new String(array);//make the array string
 		
	
		
		System.out.println(aaa);//print the complement of the new string 
	}
}





Thanks it worked.
Was This Post Helpful? 0
  • +
  • -

#8 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

Re: binary number progblem

Posted 30 September 2007 - 01:20 PM

View Postjack85, on 30 Sep, 2007 - 12:03 PM, said:

View PostPennyBoki, on 30 Sep, 2007 - 11:44 AM, said:

ok here is an example:

public class Example
{
	public static void main(String [] args)
	{
		String aaa="0100010101";//some input string
			
		char[] array;// some array of chars
		
		String tmp = new String();//tmp = temporary string 
		tmp=aaa;//give tha value of aaa to tmp
		array=tmp.toCharArray();// fill the array with the chars of the tmp string
		for(int i=0; i<aaa.length();i++)// loop through the array
		{	
			if(array[i]=='1')//if the char at that poistion is 1
			{
				array[i]='0';	//make it 0
			}
			else array[i]='1';	//if not make it 1
			
			
			
		}//end the for loop
 		aaa = new String(array);//make the array string
 		
	
		
		System.out.println(aaa);//print the complement of the new string 
	}
}





Thanks it worked.



A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.
Was This Post Helpful? 0
  • +
  • -

#9 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

Re: binary number progblem

Posted 01 October 2007 - 07:54 AM

View Postjack85, on 30 Sep, 2007 - 01:20 PM, said:

View Postjack85, on 30 Sep, 2007 - 12:03 PM, said:

View PostPennyBoki, on 30 Sep, 2007 - 11:44 AM, said:

ok here is an example:

public class Example
{
	public static void main(String [] args)
	{
		String aaa="0100010101";//some input string
			
		char[] array;// some array of chars
		
		String tmp = new String();//tmp = temporary string 
		tmp=aaa;//give tha value of aaa to tmp
		array=tmp.toCharArray();// fill the array with the chars of the tmp string
		for(int i=0; i<aaa.length();i++)// loop through the array
		{	
			if(array[i]=='1')//if the char at that poistion is 1
			{
				array[i]='0';	//make it 0
			}
			else array[i]='1';	//if not make it 1
			
			
			
		}//end the for loop
 		aaa = new String(array);//make the array string
 		
	
		
		System.out.println(aaa);//print the complement of the new string 
	}
}





Thanks it worked.



A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.


A guys can someone help me with my problem.

A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.
Was This Post Helpful? 0
  • +
  • -

#10 jack85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-September 07

Re: binary number progblem

Posted 01 October 2007 - 06:28 PM

View Postjack85, on 1 Oct, 2007 - 07:54 AM, said:

View Postjack85, on 30 Sep, 2007 - 01:20 PM, said:

View Postjack85, on 30 Sep, 2007 - 12:03 PM, said:

View PostPennyBoki, on 30 Sep, 2007 - 11:44 AM, said:

ok here is an example:

public class Example
{
	public static void main(String [] args)
	{
		String aaa="0100010101";//some input string
			
		char[] array;// some array of chars
		
		String tmp = new String();//tmp = temporary string 
		tmp=aaa;//give tha value of aaa to tmp
		array=tmp.toCharArray();// fill the array with the chars of the tmp string
		for(int i=0; i<aaa.length();i++)// loop through the array
		{	
			if(array[i]=='1')//if the char at that poistion is 1
			{
				array[i]='0';	//make it 0
			}
			else array[i]='1';	//if not make it 1
			
			
			
		}//end the for loop
 		aaa = new String(array);//make the array string
 		
	
		
		System.out.println(aaa);//print the complement of the new string 
	}
}





Thanks it worked.



A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.


A guys can someone help me with my problem.

A bro I have one more problem also. In my Decimal -> Binary. I can't reverse a number. It only work in
Binary -> Decimal function. It didn't revers at all.




Need Help
Was This Post Helpful? 0
  • +
  • -

#11 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: binary number progblem

Posted 02 October 2007 - 07:08 AM

You have to be patient, this is a forum not live chat, that means you may get answer sooner or later or maybe you wont get one at all. Instead of the time waiting for someone to reply, try solving this yourself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1