package com.codegym.task.task04.task0429;
/*
Positive and negative numbers
*/
import java.io.*;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int count = 0;
int count0 = 0;
for (int i = 0; i <= 2; i++){
if (scanner.nextInt() > 0) {
count++;
}
else if(scanner.nextInt() == 0) {
count0++;
}
}
System.out.println("Number of negative numbers: " + (3-count-count0));
System.out.println("Number of positive numbers: " + count);
}
}
Why is this not working? It works if I don't add the ==0 condition, but as soon as I add it, when the number is negative or zero it loops more than 3 times???
Resolved
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
10 December 2018, 17:03
Do you know what your code is doing? You have it reading code as part of the if block, this is not what you want it to do. Everytime you have scanner.nextInt(), it is asking for user input. Basically if the number you enter comes back false in the first if (scanner.nextInt() > 0) then it goes to the else if statement and asks for a new number. This could lead to 2 numbers being pulled for each loop, but the count never being right as when a negative number is entered first it will not increment the count0 variable. Unless that number is equal to zero that count0 never is incremented. This is what you need to do: Declare an int before the if statements to set as the user input, then use that value in the if statements (you can keep the loop the same) such as:
Then change your displayed text to:
I do like that you made the input part of a loop. If this was 'real world' and you had to update the code to count user input 1000 times instead of 3, all you have to do is change 1 number in your code and you would be done.
+3
Xubres
10 December 2018, 17:38
Thank you!
This is what I did to fix it, but I hadn't understand why it worked if I made a variable, and why it didn't work if I left it without a variable. But now I understand my mistake!
0
Guadalupe Gagnon
10 December 2018, 18:53
You know how I knew this? I made the same exact mistake in another lesson.
0
Michael Martin
10 December 2018, 00:47
You don't need a loop. Just read in 3 numbers:
0
Xubres
10 December 2018, 01:23
Thanks, but IntelliJ gives me a green line when I put 3 scanner and tells me I am using duplicate code, which I assume is bad practice and that's why it's telling me, so that's why I used a for loop.
Anyway, I already figured out how to move on by assigning the scanner to a variable and then checking the variable in the ifs, but I still don't understand why my original code isn't working.
Because the funny thing is if I remove the else if, it works for both positive and negative numbers, but when I add the else if statement it works only for positives but not for negatives or zeros and the loop loops 6 times instead of 3.
The only thing I managed to find on google is that scanner behaves in some weird ways and sometimes you need to do scan.nextLine and parse it into int. But I also tried this and all combinations of scanner.next() as well as buffered reader and nothing seems to work except assigning the input to a variable and I wonder why.
0