I
I
imperiumcat2016-02-25 13:44:53
Java
imperiumcat, 2016-02-25 13:44:53

Problems with IF in Java?

Problem conditions:
1. Enter 20 numbers from the keyboard and save them in a list
2. sort them into three lists
3. the number is divisible by 3
4. the number is divisible by 2
5. all the rest
6. if the number is divisible by both 2 and 3, then it gets into both lists
7. the printList method should print the list from a new line, each element from a new line, first 3, 2, then the rest
solution:

import java.util.*;
import java.lang.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;

public static void main(String[] args) throws IOException {
        ArrayList<Integer> input = new ArrayList<>();
        ArrayList<Integer> odd = new ArrayList<>();
        ArrayList<Integer> even = new ArrayList<>();
        ArrayList<Integer> other = new ArrayList<>();
 
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        for (int i = 0; i < 20; i++) {
            String s = reader.readLine();
            if (s.isEmpty()) {
                break;
            }
            input.add(Integer.parseInt(s));
        }
 
        fillArray(input, odd, even, other);
        printList(odd, even, other);
}
 
 
 
private static void fillArray(ArrayList<Integer> in, ArrayList<Integer> odd, ArrayList<Integer> even, ArrayList<Integer> other) {
        for (int x : in) {
            if (x % 2 == 0 & x % 3 == 0) {
                even.add(x);
                odd.add(x);
            } else if (x % 2 == 0) {
                even.add(x);
            } else if (x % 3 == 0) {
                odd.add(x);
            } else {
                other.add(x);
            }
        }
    }
 
    public static void printList(ArrayList<Integer> odd, ArrayList<Integer> even, ArrayList<Integer> other) {
        System.out.println("odd:");
        for (int j : odd) {
            System.out.println(j);
        }
        System.out.println("even:");
        for (int j : even) {
            System.out.println(j);
        }
        System.out.println("other:");
        for (int j : other) {
            System.out.println(j);
        }
    }
 

works, pifxil code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kornachev, 2016-02-25
@imperiumcat

What editor do you write in?

else if (x % 2 == 0)
 {
even.add(x);}   //<------- лишняя скобка
 }

continuation:
//Эти объекты объявлены в методе main(), и их видно только в нем.
ArrayList<Integer> even = new ArrayList<Integer>();  
ArrayList<Integer> odd = new ArrayList<Integer>();  
ArrayList<Integer> other = new ArrayList<Integer>();

//этот метод не может найти переменные even, odd, other. статический метод может видеть только другие статические методы/переменные/др и то, что передаешь в сигнатуре(в скобочках)
public static void printList(){

}

There are many options, here are 2 of them:
class Ideone{
 //выносишь переменные из метода main и делаешь их static
static ArrayList<Integer> even = new ArrayList<Integer>();  
static ArrayList<Integer> odd = new ArrayList<Integer>();  
static ArrayList<Integer> other = new ArrayList<Integer>();
}

or
//переписываешь метод, и в мейне при его вызове передаешь сви списки
public static void printList(List<Integer> even, List<Integer> odd, List<Integer> other ){

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question