Binary Calculator

Page 1 of 1

1 Replies - 15959 Views - Last Post: 21 September 2007 - 01:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=33712&amp;s=2b110e932aeab6a8dd5f48d05938b391&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 csbeginner

Reputation: 1
• Posts: 1
• Joined: 21-September 07

Binary Calculator

Posted 21 September 2007 - 12:43 AM

import java.util.Scanner;
import java.io.*;

public class BinCalc2 {

public static void main(String[] args){
String str = new String();
String str2 = new String();
int carry = 0;
int[] result = new int[4];
Scanner keyboard = new Scanner(System.in);
Str bin[] = keyboard.next();
Str bin2[] = keyboard.next();
for(int i = 2; i >= 0; i--){
int tempNum = Integer.parseInt(bin[i]);
int tempNum2 = Integer.parseInt(bin2[i]);
if((tempNum+tempNum2+carry) == 0){
result[i+1] = 0;
}else if((tempNum+tempNum2+carry) >= 2){
result[i+1] = 0;
carry = 1;
}else if((tempNum+tempNum2+carry) == 1){
result[i+1] = 1;
if(carry ==1){
carry = 0;
}
}
}
System.out.printf("%d + %d = %d", bin, bin2, result);
}
}

I keep getting errors when running the code

This post has been edited by Dark_Nexus: 21 September 2007 - 12:57 AM

Is This A Good Question/Topic? 1

Replies To: Binary Calculator

#2 1lacca

• code.rascal

Reputation: 44
• Posts: 3,822
• Joined: 11-August 05

Re: Binary Calculator

Posted 21 September 2007 - 01:46 AM

Please post the errors you are getting, thank you.