How to modify code so it writes the values in two columns?

double total = 0;

while(in.hasNextDouble()){

double value = in.nextDouble();

out.printf("%15.2f\n", value);

total = total + value;

}

out.printf("Total: %8.2f\n", total);

A short snippet of the code I'm trying to modify. It currently outputs whatever I had in an input file row by row.

double total = 0;

int count = 0;

while(in.hasNextDouble()){

if(count == 0){

double value = in.nextDouble();

out.printf("%4.2f", value);

count = 1;

total = total + value;

}

else if(count == 1){

double value = in.nextDouble();

out.printf("%15.2f\n", value);

count = 0;

total = total + value;

}

}

if(count == 1){

out.print("\n");

}

out.printf("Total: %8.2f\n", total);

I modified it that way and it works, but I feel like there is a shorter method I'm...

Read More »

By: StackOverFlow - Sunday, 11 March

Related Posts