Results 1 to 11 of 11
  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question Decimal to Binary "Using Array"

    Hi guyz !
    i tried to write an applet that gets the number from user n convert it into binary format ,however i got caught up in the array part . as u can see in below code , im need to store the reminder into an array n then display array.
    please check the code n help me on this :

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class Binary extends Applet implements ActionListener {
    
    	private Label label,label2;
    	private TextField t1,t2;
    	private Button b;
    	int m=0,n;	
    	public void init() {
    		
    	label=new Label(" Decimal ");
    	label2=new Label(" Binary ");
    	b=new Button("calculate");
    	t1=new TextField(10);
    	t2=new TextField(10);
    	
    	add(label);
    	add(t1);
    	
    	add(t2);
    	add(label2);
    	add(b);
    	
    	b.addActionListener(this);
    		
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		if (e.getSource()==b){
    			int n=Integer.parseInt(t1.getText());
    		}
    			
    			while(m!=0){
    			int m=n/2;
    			int binary[] =new int[m];
    			t2.setText(Integer.toString(binary));			
    		}
    		
    		
    	}
    		
    	}

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    In Integer class itself you are having toBinaryString() method.use it.
    Ramya:cool:

  3. #3
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    But im not gonna use library ,

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    1.Try to fix the compilation errors first.
    2.Then u put comment what u want to do.
    3.Why are u creating array each time in the loop?
    Ramya:cool:

  5. #5
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    This is the only error i got as tryin to run the proram :
    Java Code:
    cannot find symbol method toString(int[])
    AS I DECLARED IN PREVIEWS POSTS , IM TRYING TO STORE THE REMINDER OF DIVISION INTO AN ARRAY , SO I CAN DISPLAY ARRAY'S STORED VALUE. HOWEVER I HAVE NO IDEA HOW TO USE ARRAY IN THIS ONE . THATS WHY I THOUGHT I SHOULD INCLUDE ARRAY INTO while Loop . IF ITS WRONG THEN PLEASE HELP ME OUTTA THIS ! HAVE TRIED THE CODE URSELF ?

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Might be array is new to u.

    First you write the logic in comments what you want to do exactly.In the above program nothing iam able to see.
    Ramya:cool:

  7. #7
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    is there anybody else who could understand my problem ?

  8. #8
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    if i understand your problem correctly, you're trying to come up with a binary representation of an integer aren't you.

    let's try this:

    Java Code:
    int n;  // integer to work with
    int binary = new int[64];  // cause n is at most 64 bits
    int numBits = 0;
    
    while (n != 0) {
      // remainder of division by 2 gives you the lowest bit
      int nextBit = n % 2;
      // set it to the end of the array
      binary [binary.length - 1 - numBits] = nextBit;
      numBits ++;
      n /= 2;
    }
    // where did the first bit go?
    int startIndex = binary.length - numBits;
    // now print
    for (int i = 0; i < numBits; i ++) {
      System.out.print (binary [i + startIndex]);
    }
    System.out.println();

  9. #9
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    how can i put this into applet ? cause its API i guess , i need to convert it into GUI " APPLET".

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    anyway u have written a applet.Just replace acionPerformed method with this below actionPerdomed method.


    public void actionPerformed(ActionEvent ae)
    {

    int binary[] = new int[64]; // cause n is at most 64 bits
    int numBits = 0;
    String finalOutput="";
    int n=Integer.parseInt(t1.getText());

    while (n != 0) {
    // remainder of division by 2 gives you the lowest bit
    int nextBit = n % 2;
    // set it to the end of the array
    binary [binary.length - 1 - numBits] = nextBit;
    numBits ++;
    n /= 2;
    }
    // where did the first bit go?
    int startIndex = binary.length - numBits;
    // now print
    for (int i = 0; i < numBits; i ++) {
    finalOutput = finalOutput+binary [i + startIndex];
    }
    t2.setText(finalOutput);
    }
    Ramya:cool:

  11. #11
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  2. Selecting the greatest "double" in a string array
    By gangsterooseven in forum New To Java
    Replies: 6
    Last Post: 11-07-2009, 11:37 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Eclispe: "Launch failed. Binary not found"
    By qwertyuiop23 in forum Eclipse
    Replies: 1
    Last Post: 11-16-2008, 06:06 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •