Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    Initialize the result_final variable to the empty String "".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    techgeek is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default Re: binary addition and Substraction

    i would recommend you to brush up your basics at following site Home

  3. #23
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    hi again, that worked, but now the only problem is that in this part of the code i think
    jTextField1.getText();
    jTextField2.getText();
    vem=vai;
    result_final="";
    while (jTextField1.getText().isEmpty());
    {
    bin1=Integer.parseInt(jTextField1.getText());
    bin2=Integer.parseInt(jTextField2.getText());

    if ((vem==0)&(alg1==0)&(alg2==0))
    {
    vai=0;
    somabin=0;
    }

    THE PROGRAM JUST TEST THE FIRST IF, AND RETURN ALWAYS THE VALUE I PUT IN THE FIRST SOMABIN

    if (((vem==0)&(alg1==1)&(alg2==0)) | ((vem==0)&(alg1==0)&(alg2==1)))
    {
    vai=0;
    somabin=1;
    }
    THE PROGRAM JUST TEST THE FIRST IF, AND RETURN ALWAYS THE VALUE I PUT IN THE FIRST SOMABIN, sorry caps


    the original code in pascal is here, check it please, to see if i forgot something :) http://www.pcforum.com.br/cgi/yabb/Y...num=1111089082
    Last edited by m1ke4fun; 10-15-2011 at 04:26 PM.

  4. #24
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    Quote Originally Posted by m1ke4fun View Post
    hi again, that worked, but now the only problem is that in this part of the code i think

    [rubbish code deleted ... ]
    You are reading the values of those textfields in bin1 and bin2, but you are testing variable alg1 and alg2 ... there is something fishy going on there. btw, your program structure is faulty too, i.e. you don't check text fields for being empty like that. If you stole that code from a Pascal solution, remember that Java is not Pascal.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #25
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    hey again, i made a new code, with fixes, and, i can make the
    0 + 0 → 0
    0 + 1 → 1
    1 + 0 → 1
    1 + 1 → 0, carry 1
    right, but only with 1 digit, so i need to make a repetition mecanism, to check the other numbers,
    do i create a new variable to the mecanism? or use the alg1(and until it got numbers, do repetition??

  6. #26
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default Re: binary addition and Substraction

    I gave you a complete solution in a previous reply above (it didn't have the preposterous logic you copied either) but you refused it; you are on your own now.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #27
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    all i need is a variable that got the lenght of textfield1, and test it, if the lenght of textfield >=1 , it will do the if repetition, , but i am lost on that

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #29
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    its a java application
    got 2 textfields, 1 botton, 1 label
    in this code, only do the add with one digit, if i put 2 always return 0, for example 1+1=10 ,1+0=1 , 0+0=0 ;but if i put 10+10=0 ;
    i am noob, so i use if's


    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String resposta, result_final,result;
    int   n1, n2, i,alg1, alg2, somabin, vai, vem,  erro, bin1,bin2;
    
    result_final="";
    result="";
    i= (jTextField1.getText().length());
    alg1=Integer.valueOf(jTextField1.getText());
    alg2=Integer.valueOf(jTextField2.getText());
    
    if ((vem==0)&(alg1==0)&(alg2==0))
    {
    vai=0;
    somabin=0;
    }
    if (((vem==0)&(alg1==1)&(alg2==0)) | ((vem==0)&(alg1==0)&(alg2==1)))
    {
    vai=0;
    somabin=1;
    }
    if ((vem==0)&(alg1==1)&(alg2==1))
    {
    vai=1;
    somabin=0;
    }
    if ((vem==1)&(alg1==0)&(alg2==0))
    {
    vai=0;
    somabin=1;
    }
    if (((vem==1) & (alg1==1) & (alg2==0)) | ((vem==1) & (alg1==0) & (alg2==1)))
    {
    vai=1;
    somabin=0;
    }
    if ((vem==1)&(alg1==1)&(alg2==1))
    {
    vai=1;
    somabin=1;
    }
    result=String.valueOf(somabin);
    vem=vai;
    result_final=result+result_final;
    
    if (vem==1)
    {
    result_final=1+result_final;
    }
    jLabel1.setText(result_final);
    }
    Last edited by m1ke4fun; 10-15-2011 at 07:04 PM. Reason: quote tags changed to code tags

  10. #30
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: binary addition and Substraction

    I've changed your [quote] and [/quote] tags into [code] and [/code] tags to help your code retain its formatting and be more readable, but this isn't helping because you're not posting formatted code with proper indentation. Please edit the post above and change your unformatted code to properly formatted code so that we can read it.

  11. #31
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    edited, i think :)

  12. #32
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: binary addition and Substraction

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String resposta, result_final,result;
    int   n1, n2, i,alg1, alg2, somabin, vai, vem,  erro, bin1,bin2;
     
    result_final="";
    result="";
    i= (jTextField1.getText().length());
    alg1=Integer.valueOf(jTextField1.getText());
    alg2=Integer.valueOf(jTextField2.getText());
     
    if ((vem==0)&(alg1==0)&(alg2==0))
    {
    vai=0;
    somabin=0;
    }
    if (((vem==0)&(alg1==1)&(alg2==0)) | ((vem==0)&(alg1==0)&(alg2==1)))
    {
    vai=0;
    somabin=1;
    }
    if ((vem==0)&(alg1==1)&(alg2==1))
    {
    vai=1;
    somabin=0;
    }
    
    /**if ((vem==1)&(alg1==0)&(alg2==0))
    {
    vai=0;
    somabin=1;
    }
    if (((vem==1) & (alg1==1) & (alg2==0)) | ((vem==1) & (alg1==0) & (alg2==1)))
    {
    vai=1;
    somabin=0;
    }
    if ((vem==1)&(alg1==1)&(alg2==1))
    {
    vai=1;
    somabin=1;
    } */
    result=String.valueOf(somabin);
    vem=vai;
    result_final=result+result_final;
     
    if (vem==1)
    {
    result_final=1+result_final;
    }
    jLabel1.setText(result_final);
    }
    my program works fine without the comentarie part, so the carry option dont work, need help on that :s

Page 2 of 2 FirstFirst 12

Similar Threads

  1. addition
    By rithish in forum New To Java
    Replies: 4
    Last Post: 10-04-2011, 08:15 AM
  2. java binary addition
    By rockdude in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 03:01 PM
  3. Need help on my project on JAVA BINARY ADDITION
    By arneltroy in forum New To Java
    Replies: 3
    Last Post: 01-31-2009, 03:33 AM
  4. Addition of Two time
    By jithan in forum New To Java
    Replies: 5
    Last Post: 12-29-2008, 11:09 AM
  5. Binary Addition
    By Deo Favente in forum Advanced Java
    Replies: 11
    Last Post: 04-24-2008, 05:34 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
  •