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