0

Okay, so I'm trying to get the odd, even, and negative numbers using 3 separate conditions

%2=0, %2!=0, and <0

However if say the number doesn't belong to the condition I put element at [i] = null; in which I get an error message saying can't convert from int to null add cast integer

- Type mismatch: cannot convert from 
     null to int

Then if I proceed to cast the integer I get this error

Exception in thread "main" java.lang.NullPointerException
    at Server.getEven(Server.java:10)
    at Main.main(Main.java:10)

Now we haven't learned casting in my computer class and my teacher wouldn't accept my work with casting, even though is doesn't work.

I was wondering if i could do this project with storing my 3 arrays in the server and only having the single inputed array in the client

I.e my array of even numbers, array of odd numbers, array of negative numbers, all stored and printed in the server, while having the "array that the user inputed" solely in the client here is my code

Client

import java.util.Scanner;
public class Main {
    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        int [] array = new int[10];
        System.out.print("Insert the 10 values of your array.");
        for(int i=0; i<array.length; i++){
            array[i] = input.nextInt();
        }
        int[] even = Server.getEven(array);
        int[] odd = Server.getOdd(array);
        int[] neg = Server.getNeg(array);
        System.out.println("The even numbers in the array are...");
        System.out.println(even);
        System.out.println("The odd numbers in the array are...");
        System.out.println(odd);
        System.out.println("The negative numbers in the array are...");
        System.out.println(neg);

        input.close();
    }
}

Server

public class Server {
    public static int[] getEven(int[] array){
        int[] even = new int[array.length];
        for(int i=0; i<array.length; i++){
            if(array[i]%2 ==0){
                even[i] = array[i];
            }
            else
            { even[i] = null;// <-- here it asks me to do (Integer) null;
            }
        }
        return even;
    }

    public static int[] getOdd(int[] array){
        int[] odd = new int[array.length];
        for(int i=0; i<array.length; i++){
            if(array[i]%2 !=0){
                odd[i] = array[i];
            }
            else
            { odd[i] = null; // <-- here it asks me to do (Integer) null;
            }
        }
        return odd;
    }

    public static int[] getNeg(int[] array){
        int[] neg = new int[array.length];
        for(int i=0; i<array.length; i++){
            if(array[i]<0){
                neg[i] = array[i];
            }
            else
            { neg[i] = null; // <-- here it asks me to do (Integer) null;
            }
        }
        return neg;
    }

}
Adam Calcanes
  • 89
  • 1
  • 3
  • 8
  • Okay so I've tried your suggestions of doing Integer[] but now it's giving me this... The even numbers in the array are... [Ljava.lang.Integer;@3fe329eb The odd numbers in the array are... [Ljava.lang.Integer;@5ad52411 The negative numbers in the array are... [Ljava.lang.Integer;@5f3306ad – Adam Calcanes Jan 07 '14 at 00:30
  • This is because the array is being printed using the object toString method. Instead of `System.out.println(even);` use a for loop and print each elemnent. – Menelaos Jan 07 '14 at 00:32
  • 1
    Try printing using `System.out.println(Arrays.toString(yourArray))`. – Java Devil Jan 07 '14 at 00:33
  • @JavaDevil Nice one:) +1 . Further info: http://stackoverflow.com/questions/5415604/tostring-java-of-arrays – Menelaos Jan 07 '14 at 00:34
  • 1
    Do you really need the holes in your arrays? If I'm given `{3, 2, 8, 1, 6, 5, 9}` as input, I'd expect `getEven` to return `{2, 8, 6}`, not `{, 2, 8, , 6, , }`. If you don't need those holes, then that will affect the solutions we suggest. – ajb Jan 07 '14 at 00:35
  • Great that worked but now is there a way to get rid of the "null" element in the array? `[null, 2, null, 4, null, null, -2, null, 6, null]` – Adam Calcanes Jan 07 '14 at 00:37
  • as ajb said, seems I was typing that as he commented, it now produces holes in my array where it displays the actual "null" at the index – Adam Calcanes Jan 07 '14 at 00:41

6 Answers6

1

You cannot put null into an int[]. You must either use an Integer[], or a distinguished "flag value" like -1.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

Declare even and odd as Integer[] instead of int[]. This is the easiest fix in your case.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

an int will only take numbers as values Use an Integer instead which will hold numbers or null

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
0

int is a primitive type and can only take a number.

You can use Integer[] which is a wrapper object containing a primitive number. Since then your using an array of objects, you can have a null entry (null reference).

Example:

Integer[] even;

You can also specify a special value to represent null. Example: Integer.MIN_VALUE

Menelaos
  • 23,508
  • 18
  • 90
  • 155
0

To write a method that takes an array and returns an array with a subset of its elements, there's a few things you'll need to do differently. If you can use an ArrayList, that would be best, since it lets you create an array when you don't know the size beforehand. If you can't, you'll have to make two passes. First figure out how many elements you'll need in the result. Then, instead of assigning even[i], where even is the array you're going to return, you'll assign even[index] where index is a different index from the loop index, that gets incremented by 1 only when you assign an element into it.

public static int[] getEven(int[] array){
    // Count the event elements in array, and assign `count` to the count
    // (I'll let you fill in that part)

    int[] even = new int[count];
    int index = 0;
    for(int i=0; i<array.length; i++){
        if(array[i]%2 ==0){
            even[index++] = array[i];
        }
    }
    return even;
}
ajb
  • 31,309
  • 3
  • 58
  • 84
  • Okay so I tried your idea and I get this error `The even numbers in the array are... [I@efb78af` Here is what I wrote `public static int[] getEven(int[] array){ int count =0; for(int i=0; i – Adam Calcanes Jan 07 '14 at 00:53
  • @AdamCalcanes You still can't use `System.out.println` on an array. It looks like one of the answers left that in, but that was a mistake. See Java Devil's comment up above. P.S. What's the purpose of `count = count * 1`? – ajb Jan 07 '14 at 01:04
  • I used count = count*1 so that if the condition didn't happen then it just returned the same value of count i.e if the number wasn't even and the counter didn't increase then it would remain 0 because 0*1=0 or if it was "3" and it didn't increase 3*1=3 I guess I could've left a blank else{} statement but I was just rushing to answer you lol and if I can't use `System.out.println` how could i display it? – Adam Calcanes Jan 07 '14 at 01:09
  • You can display the values of an array by using a `for` loop. `for(int i = 0; i < array.length; i++) { System.out.print(array[i] + " ")}` – WonderWorld Jan 07 '14 at 01:58
0

The fundamental issue here is that you are declaring, up front, three arrays without knowing how many values they will need to hold. Sure, you know a maximum number of values they'll hold (10), but you don't know the minimum. This then affects your calls to Arrays.toString.

Using a List would be the preferred method, but if you haven't covered casting in your class then I'm guessing that lists have also not been covered yet. Why not just have your server return a String instead? (I'm purposely leaving out StringBuilders here for simplicity, again as I doubt they've been covered).

Main:

import java.util.Scanner;
public class Main {
    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        int [] array = new int[10];
        System.out.print("Insert the 10 values of your array.");
        for(int i=0; i<array.length; i++){
            array[i] = input.nextInt();
        }

        System.out.println("The even numbers in the array are...");
        System.out.println(Server.getEven(array));
        System.out.println("The odd numbers in the array are...");
        System.out.println(Server.getOdd(array));
        System.out.println("The negative numbers in the array are...");
        System.out.println(Server.getNeg(array));

        input.close();
    }
}

Server

public class Server {
    public static String getEven(int[] array){
        String result = "";
        for(int i=0; i<array.length; i++){
            if(array[i]%2 ==0){
                if(result.length() != 0)
                {
                    result = result + ", ";
                }
                result = result + array[i];
            }
        }
        return result;
    }

    public static String getOdd(int[] array){
        String result = "";
        for(int i=0; i<array.length; i++){
            if(array[i]%2 !=0){
                if(result.length() != 0)
                {
                    result = result + ", ";
                }
                result = result + array[i];
            }
        }
        return result;
    }

    public static String getNeg(int[] array){
        String result = "";
        for(int i=0; i<array.length; i++){
            if(array[i]<0){
                if(result.length() != 0)
                {
                    result = result + ", ";
                }
                result = result + array[i];
            }
        }
        return result;
    }
}
Catchwa
  • 5,845
  • 4
  • 31
  • 57
  • Thanks a lot, that's a really easy fix, I didn't have to change too much code around and based on what I've been taught up to date this works perfectly, as you're right I'm not too familiar with casting and lists. – Adam Calcanes Jan 07 '14 at 01:01