Not working. Any help :/

asked 30 Jun '15, 05:45

Java%20Programmer's gravatar image

Java Programmer
111
accept rate: 0%


public class Solution

{ public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    ArrayList<Integer> list = new ArrayList<Integer>();   //create large list
    ArrayList<Integer> list1 = new ArrayList<Integer>();  // create 3 other lists
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    ArrayList<Integer> list3 = new ArrayList<Integer>();

    for(int i = 0; i < 20; i++)   // for-loop to write from keyboard in the big list
    {
        String s  = reader.readLine();
        int x = Integer.parseInt(s);

        list.add(x);
    }

    for(int i = 0; i < list.size(); i++) // for-loop to write the numbers in the right list
    {
        int x = list.get(i);
        if(x%2 == 0 && x%3 == 0)
        {
            list1.add(x);
            list2.add(x);
        }
        else if(x%3 == 0)
        {
            list1.add(x);
        }
        else if(x%2 == 0)
        {
            list2.add(x);
        }
        else
        {
            list3.add(x);
        }
    }

    printList(list1);  // print and choose the lists correct as in the description

    printList(list2);

    printList(list3);

}

public static void printList(List<Integer> list)
{
    for(int i = 0; i < list.size(); i++)  //implement printList method
    {
        System.out.println(list.get(i));
    }
    System.out.println();
}

}

link

answered 02 Jun '16, 17:08

Joschi's gravatar image

Joschi
1
accept rate: 0%

Post your code which doesnt't work.

link

answered 14 Apr, 09:20

noelo's gravatar image

noelo
102
accept rate: 0%

Passed testing:

public class Solution
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<Integer> list = new ArrayList<Integer>();
        ArrayList<Integer> list1 = new ArrayList<Integer>();
        ArrayList<Integer> list2 = new ArrayList<Integer>();
        ArrayList<Integer> list3 = new ArrayList<Integer>();
        for(int i=0; i < 20; i++){
            list.add(Integer.parseInt(scan.readLine()));
        }

        for(int i=0; i < list.size(); i++){
            int n = list.get(i);
            if(n%3 == 0){
                list1.add(n);
            }
            if(n%2 == 0){
                list2.add(n);
            }
            if(n%3 != 0 && n%2 != 0){
                list3.add(n);
            }
        }
        printList(list1);
        printList(list2);
        printList(list3);
    }

    public static void printList(List<Integer> list)
    {
        for(int element : list){
            System.out.println(element);
        }

    }
}
link

answered 24 May, 17:39

Igor's gravatar image

Igor
112
accept rate: 0%

edited 24 May, 17:40

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×6

Asked: 30 Jun '15, 05:45

Seen: 2,588 times

Last updated: 24 May, 17:40