JonathanS JonathanS - 2 months ago 7
Java Question

Java error connecting variables

I am very very new to java and just trying to understand a few things better. I came up with this bit of java a few hours ago. Thanks for any advice.
I need to figure out how to basically take the

int
values in the
if
statement if they ==1 to another method to calculate the total number

Example 1111111111111111
will = 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

NEED TO have a method that can grab those values and ADD.
been messing around for a hour or two trying to hack it out.

Just messing around with code, that is all

package binChange;
import java.util.Scanner;

public class binChange {
public static void main(String args[]){
Scanner input = new Scanner (System.in);
System.out.println("Enter in only 1 and 0 and click enter for each time");
int bin1 = input.nextInt();
int bin2 = input.nextInt();
int bin3 = input.nextInt();
int bin4 = input.nextInt();
int bin5 = input.nextInt();
int bin6 = input.nextInt();
int bin7 = input.nextInt();
int bin8 = input.nextInt();
int bin9 = input.nextInt();
int bin10 = input.nextInt();
int bin11 = input.nextInt();
int bin12 = input.nextInt();
int bin13 = input.nextInt();
int bin14 = input.nextInt();
int bin15 = input.nextInt();
int bin16 = input.nextInt();


if (bin16 == 1){
System.out.println(32768);
int abin = 32768;

}
else{
System.out.println(0);
int abino = 0;
}
if (bin15 == 1){
System.out.println(16384);
int bbin = 16384;
}
else{
System.out.println(0);
int bbinO = 0;
}
if (bin14 == 1){
System.out.println(8192);
int cbin = 8192;
}
else{
System.out.println(0);
int cbino = 0;
}
if (bin13 == 1){
System.out.println(4096);
int dbin = 4096;
}
else{
System.out.println(0);
int dbino = 0;
}
if (bin12 == 1){
System.out.println(2048);
int ebin = 2048;
}
else{
System.out.println(0);
int ebino = 0;
}
if (bin11 == 1){
System.out.println(1024);
int fbin = 1024;
}
else{
System.out.println(0);
int fbino = 0;
}
if (bin10 == 1){
System.out.println(512);
int gbin = 512;
}
else{
System.out.println(0);
int gbino = 0;
}
if (bin9 == 1){
System.out.println(256);
int hbin= 256;
}
else{
System.out.println(0);
int hbino = 0;
}
if (bin8 == 1){
System.out.println(128);
int ibin = 128;
}
else{
System.out.println(0);
int ibino = 0;
}
if (bin7 == 1){
System.out.println(64);
int jbin = 64;
}
else{
System.out.println(0);
int jbino = 0;
}
if (bin6 == 1){
System.out.println(32);
int kbin = 32;
}
else{
System.out.println(0);
int kbino = 0;
}
if (bin5 == 1){
System.out.println(16);
int lbin = 16;
}
else{
System.out.println(0);
int lbino = 0;
}
if (bin4 == 1){
System.out.println(8);
int mbin = 8;
}
else{
System.out.println(0);
int mbino = 0;
}
if (bin3 == 1){
System.out.println(4);
int nbin = 4;
}
else{
System.out.println(0);
int nbino = 0;
}
if (bin2 == 1){
System.out.println(2);
int obin = 2;
}
else{
System.out.println(0);
int obino = 0;
}
if (bin1 == 1){
System.out.println(1);
int pbin = 1;
}
else{
System.out.println(0);
int pbino = 0;
}



}


}

Answer

Create a integer variable and initialize it with zero. Add the values whenever conditions are met.

int sum = 0;//Declare before first `if` statement.

Use this to add if conditions are satisfied in the if statements.For example:

if (bin16 == 1){
    System.out.println(32768);
    //int abin = 32768;//you might not need this statement.  
    sum = sum + 32768;//This 

}

So at the end of the program, the total sum can be retrieved by calling the sum variable. For example as

System.out.println("Total sum = "+sum);