-1

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.

cuphead
  • 7
  • 3
  • 1
    That's not a compile-time error. – Andrew Li Feb 18 '18 at 17:41
  • I don't see any way you can be getting that (run-time) error - you should get a different (compile-time) error before that point, specifically on `array = new array [n];`. Try to post a [mcve] including the exact code you tried to compile in future. – Bernhard Barker Feb 18 '18 at 17:44
  • 1
    `array = new array [n];` - does it even compile? – lexicore Feb 18 '18 at 17:44

3 Answers3

1

Your array initialization is not correct here.. Just replace with this

 array = new int [n];
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Bimlendu Kumar
  • 226
  • 2
  • 17
  • Right, I actually wrote the names of variables in my own native language and translated them here for everyone to understand (so basically just used different names but the same code otherwise) and then made a mistake when editing. I figured out the issue, I wasn't typing out the inputs correctly myself. Now it actually works properly. – cuphead Feb 18 '18 at 18:12
1

This is not a very big question. Since you used input.nextInt(). which means you except next input will be an Integer, and the java will convert it as well. However, if the input is not Integer then it will throws a java.util.InputMismatchException. What you need to do just check if the input is an Integer, if it is then convert it. Otherwise show something else

Also you should initialize you array with int[] array = new int[n]

Chongju Mai
  • 127
  • 1
  • 8
0

You syntax for declaring an array is wrong:

It is(For integer array):

    int array = new int[5];

And(For String array):

    String array = new String[5];

where 5 is size of array.

in your case it is:

    int array = new int[n];    
Dipanshu Mahla
  • 152
  • 1
  • 16