Welcome to Dream.In.Code
Become a Java Expert!

Join 149,608 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,893 people online right now. Registration is fast and FREE... Join Now!




Adding in Java

 
Reply to this topicStart new topic

Adding in Java, Carrying over while adding binary numbers in Java

codur07
21 Sep, 2007 - 01:12 AM
Post #1

New D.I.C Head
*

Joined: 21 Sep, 2007
Posts: 1


My Contributions
I have been trying to create a code that will add 2 strings of 3-bit binary. (e.g. 101 + 011 = 1000). I have been able to develop the majority of the code, I'm just having trouble with Carrying over when adding 1 + 1. I would really appreciate your advice on this issue.

Thank you,

Below is a copy of the code I have been working on.
(Comments are included in java code)


CODE

// This Java program should be able to add up to 3-bit binary numbers
import java.util.Scanner;

public class BinaryCalc{

    public static void main(String[] args) {
    
    Scanner keyboard = new Scanner(System.in);
    
    System.out.println("Enter first 3-bit sequence");
    String x = keyboard.next();
    
    System.out.println("Enter second 3-bit sequence");
    String y = keyboard.next();
    //By doing the following I intend to use each X digit individually with it's corresponding Y digit
    char single_character;
    single_character=x.charAt(0);
    single_character=x.charAt(1);
    single_character=x.charAt(2);
    single_character=y.charAt(0);
    single_character=y.charAt(1);
    single_character=y.charAt(2);
    //The following System.out.println is where i'm having trouble, I'm not sure how to tell Java to carry over properly and tell it that 1 + 1 = 10
    System.out.println(x+"+"+y+"="+(x+y));
    }
}


User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Adding In Java
21 Sep, 2007 - 09:22 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi I'm afraid (not really) that your program doesn't do the job you want it to.

The only adding you do is actually concatenating strings.
What is the thing you try to do with the single_character variable?

The solution is pretty easy since you have the limitation of 3bit sequence.

My suggestion is compare the last char(third) of the first string, with the last char of the second string, then do something, the same thing goes for the middle bit, but you should bear in mind if the previous comparing was comparing of two 1, the first bit comparing should go exactly as the second(middle), so the result keep it in an char array perhaps...

Now do you know how to add binaries? if no, then back to the drawing board.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:05AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month