简体   繁体   中英

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

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM