Living and non living java sba

There are more available on https://play.google.com/store/apps/details?id=com.master.askmastermaterial


Living and Non Living StringList:

Question:

*We must use two arraylist ,where we have living things on one list and non-living things on another list.

*We need to get one search string from console and have to compare with both list

*print the statement accoridngly.like 《your string》 is a living thing!

《your string》 is a non living thing!

《your string》 is not found on  oth lists.

Sample input:

Car

Sample output:

Car is a  non-living thing.

Solution:


 import java.util.ArrayList;

import java.util.Iterator;

public class Main{

   public static void main(String[] args){

      ArrayList <String> list = new ArrayList<String>();

ArrayList <String> list1 = new ArrayList<String>();

      //Instantiating an ArrayList object

      list.add("car");

      list.add("bike");

      list1.add("penguin");

Scanner in = new Scanner(System.in);

     str = in.nextLine();

      for (String element : list){

         if (element.contains(str)){

               System.out.println(str+" is a non-living thing!");

System.exit(0);

         }

      }

for (String element : list1){

         if (element.contains(str)){

               System.out.println(str+" is a living thing!");

System.exit(0);

         }

      }

System.out.println(str+" is not found in both lists.");

System.exit(0);

   }

}

End :

Not executed please verify from your side.



Comments

Popular posts from this blog

Cognizant Html css js CC

Java sba