I am fairly new to programming in general but I am trying to learn as much as I can.
So, I've been doing this little project where the program asks the user to enter a certain amount of numbers (n) and then, at the end, the program writes the message about how many positive, negative, even and odd numbers there are depending on the input.
This is my code:
package zad14;
import java.util.Scanner;
public class Zad14 {
public static void main(String[] args) {
int n;
int totalEven = 0;
int totalOdd = 0;
int totalPositive = 0;
int totalNegative = 0;
int [] array;
Scanner input = new Scanner (System.in);
System. out.print ("Insert the amount of numbers: ");
n = input.nextInt();
array = new array [n];
for (int i = 0; i < n; i++){
System.out.print ("Enter the numbers: ");
array [i] = input.nextInt();
}
for (int i = 0; i < n; i++) {
if ( array [i] <0) {
totalNegative ++;
}
if (array [i] > 0) {
totalPositive++;
}
if (array [i] %2 == 0) {
totalEven++;
}
if (array [i] %2 == 1) {
totalOdd++;
}
}
System.out.println ("Even numbers: " + totalEven);
System.out.println ("Odd numbers " + totalOdd);
System.out.println ("Positive numbers " + totalPositive);
System.out.println ("Negative numbers: " + totalNegative);
}
}
And then when I compile I get the following error:
"Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at zad14.Zad14.main(Zad14.java:32)"
What is done wrong here? Any help would be very appreciated. Also btw, I am using NetBeans if that means something.