Adam Calcanes
Adam Calcanes

Reputation: 89

How to convert null to int without casting to integer?

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;
    }

}

Upvotes: 0

Views: 6108

Answers (6)

Catchwa
Catchwa

Reputation: 5855

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;
    }
}

Upvotes: 0

ajb
ajb

Reputation: 31699

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;
}

Upvotes: 0

Menelaos
Menelaos

Reputation: 26299

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

Upvotes: 0

Glenn Teitelbaum
Glenn Teitelbaum

Reputation: 10343

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

Upvotes: 0

peter.petrov
peter.petrov

Reputation: 39477

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

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198211

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

Upvotes: 1

Related Questions