ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Java - Read and Print N names using Vector in Java.


IncludeHelp 11 September 2016

In this code snippet/example we will learn how to read and print N names (strings) using Vector in Java?

This is an example of Vector in Java, this example will read and print N strings (names) using Java Victor.

To print all vector elements, we are using Enumeration.

Java Code Snippet - Read and Print N Names using Vector in Java (Example of Vector)

//Java - Read and Print N names using Vector in Java

import java.util.*;
import java.util.Scanner;
 
class ExampleVector
{
	public static void main(String args[])
	{
		Vector names;
		names=new Vector();
		String text=null;
		int choice=0;
		
		Scanner SC=new Scanner(System.in);
		
		while(true){
			System.out.print("Enter name: ");
			text=SC.next();
			//add into vector
			names.add(text);
			
			System.out.print("More... (0 to exit): ");
			choice=SC.nextInt();
			if(choice==0)
				break;
		}
		
		//display all names
		Enumeration en=names.elements();
		
		System.out.println("All entered names are:");
		
		while(en.hasMoreElements()){
			System.out.println(en.nextElement());
		}				
	}
}
    
    Enter name: Mike
    More... (0 to exit): 1
    Enter name: Priya
    More... (0 to exit): 1
    Enter name: Alak
    More... (0 to exit): 0
    All entered names are:
    Mike
    Priya
    Alak


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.