Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: erase

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default erase

    BigInteger PowerOfTwo = BigInteger.valueOf(2);
              DecimalToBinary = new BigInteger(LargeBase.getText());
              String binaryString = DecimalToBinary.toString(2);
              int binaryNo = Integer.parseInt(binaryString, 2); 
    	  String backTOString = Integer.toBinaryString(binaryNo);
              char[] binaryArray = backTOString.toCharArray();
              int FristCount=0;int SecCount=0;
              for (char c : binaryArray){
                  if(c == 49){ // int i=c; will convter to ascii, so 1=49, 0=48.
     
                      display.setText(display.getText()+" "+PowerOfTwo+"^"+FristCount+" + ");}
                      FristCount++;
          }
    this code is working, however the print out want to be 2+2.
    However the print out come as 2+2+.
    How to earse the last part of +
    so come it will cone out like 2+2.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: erase

    It might help if you explained what the code is supposed to be doing...

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: erase

    display.setText(display.getText()+" "+PowerOfTwo+"^"+FristCount+" + ");}
    Is this the code that adds the ending +?
    You need change the logic to put the + at the beginning of the concatenation vs at the end as you have here.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: erase

    Your code will always print an extra " + ", you can't just erase it.
    You can fix this by putting in an if() statement to check if there is more numbers to output.

    if(c == 49){ // int i=c; will convter to ascii, so 1=49, 0=48.

    display.setText(display.getText()+" "+PowerOfTwo+"^"+FristCount);
    if( //more to print)
    display(" + ");
    }

    FristCount++;

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: erase

    Why not use:
    if(c == '1')

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: erase

    Quote Originally Posted by Norm View Post
    Why not use:
    if(c == '1')
    c is char, when char convert to int. it will change to 49, when char is 1.

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: erase

    Quote Originally Posted by firebluetom View Post
    Your code will always print an extra " + ", you can't just erase it.
    You can fix this by putting in an if() statement to check if there is more numbers to output.

    if(c == 49){ // int i=c; will convter to ascii, so 1=49, 0=48.

    display.setText(display.getText()+" "+PowerOfTwo+"^"+FristCount);
    if( //more to print)
    display(" + ");
    }

    FristCount++;
    how to use if statement to check?