Ivan Chafardeth
Ivan Chafardeth

Reputation: 11

Array List for odd and even numbers stopping at -1

I can't get my code to work. Im trying to get all the even and odd numbers of an array but I have to stop whenever i find a -1.

import java.util.*;
public class EvenOdd{
  private static int[] array;
  private static List<Integer> even = new ArrayList<>();
  private static List<Integer> odd = new ArrayList<>();
  Scanner sc = new Scanner();
  int numbers = sc.nextInt();

  public static void classify() {
    for(int i = 0 ; i < array.length ; i++){
      if(numbers==-1){
        if(array[i] % 2 == 0)
            even.add(array[i]);

        else
            odd.add(array[i]);
      }
    }
  }

  public static void display(List<Integer> list){
    for(Integer i : list)
        System.out.println(i);
  }

  public static void main(String[] args){
    classify();
    display(even);
  }
}

Upvotes: 0

Views: 4138

Answers (3)

Aroniaina
Aroniaina

Reputation: 1252

There are many error in your code

  • constructor of java.util.Scanner
  • the if(numbers == -1) condition
  • the attribut array is allways null

This code is a small improvement of your code

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class EvenOdd{
    private static List<Integer> array;
    private static List<Integer> even;
    private static List<Integer> odd;
    private static Scanner sc;

    public EvenOdd() {
        array = new ArrayList<>();
        even = new ArrayList<>();
        odd = new ArrayList<>();
        sc = new Scanner(System.in);
    }


    public void classify(){
        for(int n : array){
            if(n % 2 == 0) even.add(n);
            else odd.add(n);
        }
    }

    public void fillArray(){
        int n = sc.nextInt();
        do { 
            array.add(n); 
            n = sc.nextInt();
        } while (n != -1);
    }

    public void display(List<Integer> list){
        for(Integer i : list)
            System.out.println(i);
    }

    public List<Integer> getEven() {
        return even;
    }

    public List<Integer> getOdd() {
        return odd;
    }

    public static void main(String[] args){
        EvenOdd eo = new EvenOdd();
        eo.fillArray();
        eo.classify();
        System.out.println("even");
        eo.display(eo.getEven());
        System.out.println("odd");
        eo.display(eo.getOdd());
    }
}

Upvotes: 0

jenjis
jenjis

Reputation: 1097

There are many issues in your code:

First of all, your code evaluates the next integer only if it is -1, skipping iteration otherwise;

Second, you're making a bit of confusion on the classify operation input: the array was never populated, so the use of array in the for statement and module operations does not have any sense. Furthermore the scanner constructor does not have a source.

If you want to insert numbers to evaluate from System.in, you can try the following fixed code:

import java.util.*;
public class Main {

    private static List<Integer> even = new ArrayList();
    private static List<Integer> odd = new ArrayList();

    public static void classify(Scanner sc){
        Integer number;
        while(sc.hasNextInt()) {
            number = sc.nextInt();
            if(number==-1)
                break;
            if(number % 2 == 0)
                even.add(number);
            else
                odd.add(number);

        }
    }

    public static void display(List<Integer> list){
        for(Integer i : list)
            System.out.println(i);
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        classify(sc);
        display(even);
    }
}

Upvotes: 1

Anfelipe
Anfelipe

Reputation: 771

For what I've just read in the docs the Scanner class constructor needs a source as a parameter, and in your line:

Scanner sc = new Scanner(/*insert_source_object_here*/);

You are not providing any source, here's the list of possible constructors:

Scanner(File source)
Scanner(File source, String charsetName)
Scanner(InputStream source)
Scanner(InputStream source, String charsetName)
Scanner(Path source)
Scanner(Path source, String charsetName)
Scanner(Readable source)
Scanner(ReadableByteChannel source)
Scanner(ReadableByteChannel source, String charsetName)
Scanner(String source)

Upvotes: 0

Related Questions