Question

In: Computer Science

Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...

Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.

Solutions

Expert Solution

CODE:

import java.util.*;
class Array_List{
   public static void main(String[] args) {
       Scanner scan=new Scanner(System.in);
       ArrayList<Integer> num1=new ArrayList<Integer>();
       ArrayList<Integer> evennum=new ArrayList<Integer>();
       int n,count=0;
      
       System.out.print("Enter num:");
           n=scan.nextInt();
       while(n>0)
       {   count+=1;
           num1.add(n);
           System.out.print("Enter num:");
           n=scan.nextInt();
       }
       for(Integer numb:num1)
       {
           if(numb%2==0)
           {
               evennum.add(numb);
           }
       }
       System.out.println("evennum list is:"+evennum);
  
   }
}

OUTPUT:

If you have any doubts please COMMENT...
If you understand the answer please give THUMBS UP...


Related Solutions

In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]] [ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c] assuming abc are integer Thanks in advance!
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard by displaying the message on the screen to ask and read the following information: * customer ID (string) * customer name (string)                                                        * balance (float) -open output file customer .txt to write -Write to the output file customer.txt the following information:                 Customer ID – customer name – balance For example: 1561175753 - James Smith – 1255.25 -close file QUESTION 5: -create one notepad...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm =...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>(); for (int i= 0; i<2; i++) { al.add(i); al.add(i+1); hs.add(i); hs.add(i+1); hm.put(al.get(i), al.get(i+1)); hm.put(hm.get(i), hm.get(i+1)); }   System.out.println(al); System.out.println(hs); System.out.println(hm); // {key=value}. ---------------------------------- What output is produced by the following code and why?
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
Write an interactive Java class which accepts an input argument when the application is executed from...
Write an interactive Java class which accepts an input argument when the application is executed from the command-line. Accept input from the user and compare the value entered to the command-line argument value. If the strings do not equal, display "INVALID VALUE! TRY AGAIN!", otherwise display "PERMISSION GRANTED!" and exit the program.
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT