http://www.technicalpage.net/search/label/SQL

AddToArrayListOneByOne

 Add words/elements to the ArrayList, one by one

package abc;

 

import java.util.ArrayList;

import java.util.Scanner;

 

public class EnterStringInForLoop {

 

       public static void main(String[] args) {

            

             Scanner scnr = new Scanner(System.in);

            

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

             System.out.println("Enter the 5 days names: ");

             for(int i=0;i<5;i++) {

                    String days = scnr.nextLine();/////nextLine

                    alist2.add(days);

             }

             System.out.println("Scanner nextLine :"+alist2);

            

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

             System.out.println("Enter the 5 state names: ");

             for(int i=0;i<5;i++) {

                    String state = scnr.next();/////next

                    alist1.add(state);

             }

             System.out.println("Scanner next :"+alist1);

            

 

            

             //Another Way(asking for each entry)

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

             for(int i=0;i<5;i++) {

                    System.out.println("Enter the 5 Months, Month"+(i+1));

                    String state = scnr.next();

                    alist3.add(state);

             }

             System.out.println(alist3);

 

       }

 

}

 

Output:

Enter the 5 days names:

d

d

d

d

d

Scanner nextLine :[d, d, d, d, d]

Enter the 5 state names:

d

d

d

d

d

Scanner next :[d, d, d, d, d]

Enter the 5 Months, Month1

 

d

Enter the 5 Months, Month2

d

Enter the 5 Months, Month3

d

Enter the 5 Months, Month4

d

Enter the 5 Months, Month5

d

[d, d, d, d, d]

 

It does not work if you change the sequence

package abc;

 

import java.util.ArrayList;

import java.util.Scanner;

 

public class EnterStringInForLoop {

 

       public static void main(String[] args) {

            

             Scanner scnr = new Scanner(System.in);

            

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

             System.out.println("Enter the 5 state names: ");

             for(int i=0;i<5;i++) {

                    String state = scnr.next();/////next

                    alist1.add(state);

             }

             System.out.println("Scanner next :"+alist1);

            

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

             System.out.println("Enter the 5 days names: ");

             for(int i=0;i<5;i++) {

                    String days = scnr.nextLine();/////nextLine

                    alist2.add(days);

             }

             System.out.println("Scanner nextLine :"+alist2);

            

             //Another Way(asking for each entry)

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

             for(int i=0;i<5;i++) {

                    System.out.println("Enter the 5 Months, Month"+(i+1));

                    String state = scnr.next();

                    alist3.add(state);

             }

             System.out.println(alist3);

 

       }

 

}

 

Output:

Enter the 5 state names:

d

d

d

d

d

Scanner next :[d, d, d, d, d]

Enter the 5 days names:

d

d

d

d

Scanner nextLine :[, d, d, d, d]

Enter the 5 Months, Month1

d

Enter the 5 Months, Month2

d

Enter the 5 Months, Month3

d

Enter the 5 Months, Month4

d

Enter the 5 Months, Month5

d

[d, d, d, d, d]

 

 

No comments:

Post a Comment