On my code, it says that there are 2 mistakes and I can't fix them.

Here is my code:

import java.util.Scanner;

public class Exercise4_1M
{
public static void main(String[] args)
{
int countPositive = 0; int countNegative = 0;
int count = 0; int total = 0;

Scanner input = new Scanner(System.in);
System.out.print(
"Enter an integer, the input ends if it is 0: ");
int num = input.nextInt();

while (num != 0) {
if (num > 0)
countPositive++;
else if (num < 0) {
countNegative++;
}
total += num;
count++;

num = input.nextInt();
}

if (count == 0) {
System.out.println("No numbers were entered except 0");
} else {
System.out.println("The number of positives is " + countPositive);
System.out.println("The number of negatives is " + countNegative);
System.out.println("The total is " + total);
System.out.println("Average is " + total * 1.0D / count);
}
}
}

And this is error log 1:

You got this one wrong test case 2
First Difference Occurs at: byte 119, line 3
On Line 3 its column 15
Line 3 Wrong: The total is 1533672726
Line 3 Right: The total is 10123607318 

sdiff side by side difference your output versus solution....
Enter an integer, the input ends if it is 0: The number of po   Enter an integer, the input ends if it is 0: The number of po
The number of negatives is 2                    The number of negatives is 2
The total is 1533672726                       | The total is 10123607318
Average is 8.071961715789473E7                    | Average is 532,821,437.79

Octal Dump Difference
0000160   l       i   s       1   5   3   3   6   7   2   7   | 0000160   l       i   s       1   0   1   2   3   6   0   7  
0000200   A   v   e   r   a   g   e       i   s       8   .   | 0000200  \n   A   v   e   r   a   g   e       i   s       5  

Octal Dump Your output...
0000000   E   n   t   e   r       a   n       i   n   t   e   g   e   r
0000020   ,       t   h   e       i   n   p   u   t       e   n   d   s
0000040       i   f       i   t       i   s       0   :       T   h   e
0000060       n   u   m   b   e   r       o   f       p   o   s   i   t
0000100   i   v   e   s       i   s       1   7  \n   T   h   e       n
0000120   u   m   b   e   r       o   f       n   e   g   a   t   i   v
0000140   e   s       i   s       2  \n   T   h   e       t   o   t   a
0000160   l       i   s       1   5   3   3   6   7   2   7   2   6  \n
0000200   A   v   e   r   a   g   e       i   s       8   .   0   7   1
0000220   9   6   1   7   1   5   7   8   9   4   7   3   E   7  \n
0000237

Octal Dump Solution..
0000000   E   n   t   e   r       a   n       i   n   t   e   g   e   r
0000020   ,       t   h   e       i   n   p   u   t       e   n   d   s
0000040       i   f       i   t       i   s       0   :       T   h   e
0000060       n   u   m   b   e   r       o   f       p   o   s   i   t
0000100   i   v   e   s       i   s       1   7  \n   T   h   e       n
0000120   u   m   b   e   r       o   f       n   e   g   a   t   i   v
0000140   e   s       i   s       2  \n   T   h   e       t   o   t   a
0000160   l       i   s       1   0   1   2   3   6   0   7   3   1   8
0000200  \n   A   v   e   r   a   g   e       i   s       5   3   2   ,
0000220   8   2   1   ,   4   3   7   .   7   9  \n
0000233

Help On Intpreting Output Better luck next time!

And here is error log 2:

First Difference Occurs at: byte 140, line 4
On Line 4 its column 16
Line 4 Wrong: Average is -124975.0
Line 4 Right: Average is -124,975.00 

sdiff side by side difference your output versus solution....
Enter an integer, the input ends if it is 0: The number of po   Enter an integer, the input ends if it is 0: The number of po
The number of negatives is 5                    The number of negatives is 5
The total is -999800                        The total is -999800
Average is -124975.0                          | Average is -124,975.00

Octal Dump Difference
0000200   a   g   e       i   s       -   1   2   4   9   7   | 0000200   a   g   e       i   s       -   1   2   4   ,   9  
0000220  \n                           | 0000220   0   0  \n

Octal Dump Your output...
0000000   E   n   t   e   r       a   n       i   n   t   e   g   e   r
0000020   ,       t   h   e       i   n   p   u   t       e   n   d   s
0000040       i   f       i   t       i   s       0   :       T   h   e
0000060       n   u   m   b   e   r       o   f       p   o   s   i   t
0000100   i   v   e   s       i   s       3  \n   T   h   e       n   u
0000120   m   b   e   r       o   f       n   e   g   a   t   i   v   e
0000140   s       i   s       5  \n   T   h   e       t   o   t   a   l
0000160       i   s       -   9   9   9   8   0   0  \n   A   v   e   r
0000200   a   g   e       i   s       -   1   2   4   9   7   5   .   0
0000220  \n
0000221

Octal Dump Solution..
0000000   E   n   t   e   r       a   n       i   n   t   e   g   e   r
0000020   ,       t   h   e       i   n   p   u   t       e   n   d   s
0000040       i   f       i   t       i   s       0   :       T   h   e
0000060       n   u   m   b   e   r       o   f       p   o   s   i   t
0000100   i   v   e   s       i   s       3  \n   T   h   e       n   u
0000120   m   b   e   r       o   f       n   e   g   a   t   i   v   e
0000140   s       i   s       5  \n   T   h   e       t   o   t   a   l
0000160       i   s       -   9   9   9   8   0   0  \n   A   v   e   r
0000200   a   g   e       i   s       -   1   2   4   ,   9   7   5   .
0000220   0   0  \n
0000223

Help On Intpreting Output Better luck next time!

I am not sure how to fix this, the code seems right to me.

Recommended Answers

All 6 Replies

You should notice this line:

Line 3 Right: The total is 10123607318 

That is one big number. In fact, it is larger than what you can represent with a 32bit integer type, which is what int is in Java (the number is about 10 billion, and the max for an int is about 2 billion). This is an integer overflow problem. You need to sum-up the total into a larger integer than that, like long.

The second problem is about formatting only:

Line 4 Wrong: Average is -124975.0
Line 4 Right: Average is -124,975.00 

You have your output set to putting a comma between thousands. You need to change that setting. I think you can just do:

 DecimalFormat myFormatter = new DecimalFormat("#.0#");
System.out.println("Average is " + myFormatter.format(total * 1.0D / count));
commented: Thank you! +0

Thanks Mike!

So i tried out what you said, the best that i can, and ended up with this:

import java.util.Scanner;

public class Exercise4_1M
{
public static void main(String[] args)
{
long countPositive = 0; long countNegative = 0;
long count = 0; long total = 0;
DecimalFormat myFormatter = new DecimalFormat("#.0#");

Scanner input = new Scanner(System.in);
System.out.print(
"Enter an integer, the input ends if it is 0: ");
long num = input.nextInt();

while (num != 0) {
if (num > 0)
countPositive++;
else if (num < 0) {
countNegative++;
}
total += num;
count++;

num = input.nextInt();
}

if (count == 0) {
System.out.println("No numbers were entered except 0");
} else {
System.out.println("The number of positives is " + countPositive);
System.out.println("The number of negatives is " + countNegative);
System.out.println("The total is " + total);

System.out.println("Average is " + myFormatter.format(total * 1.0D / count));
}
}
}

But the problem now is that i am getting an error with the decimal format? It just says, error: cannot find symbol-----> DecimalFormat myFormatter = new DecimalFormat("#.0#");

I guess you have to add this line at the top:

import java.text.DecimalFormat;
commented: ... i now feel dumb. :D +9

I guess you have to add this line at the top:
import java.text.DecimalFormat;

... now i feel dumb... but it worked!

Last question, i am kind of lost on this one.
How do you put a comma in between each hundreds place.

Instead of 123456.78
It would be 123,456.78

It is not required for us to do it that way, but I am kind of curious to know how it is done.

Just read the docs. It's simply:

DecimalFormat myFormatter = new DecimalFormat("#,##0.0#");
commented: Thanks :D +0

Thanks :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.