0
6.4kviews
Write a program to read five names of students from command line and store them in a vector. Sort list in alphabetical order and display using Enumeration interface.

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 10M

Year: Dec 2015

2
    import java.util.*;
    class Enum
    {
        public static void main(String args[])
        {
            Vector v=new Vector();
            v.add(args[0]);
            v.add(args[1]);
            v.add(args[2]);
            v.add(args[3]);
            v.add(args[4]);
            Enumeration e=v.elements();
            System.out.println("BEFORE SORTING : ");
            while(e.hasMoreElements())
            {
                System.out.println(e.nextElement());
            }

            Collections.sort(v);
            Enumeration e1=v.elements();
            System.out.println("After SORTING : ");
            while(e1.hasMoreElements())
            {
                System.out.println(e1.nextElement());
            }
        }
    }
BEFORE SORTING :
f
t
y
e
i
After SORTING :
e
f
i
t
y

1 Answer
0
73views
package com.java2novice.vector;
import java.util.Iterator;
import java.util.Vector;
public class VectorIterator {
    public static void main(String a[]){
        Vector<String> vct = new Vector<String>();
           for(int i=0; i<5; i++)
{
Scanner scanner = new Scanner(System.in);
    System.out.print("Enter student  name: ");
    String studentName = scanner.next();
vct.add(studentName );
} 
         Iterator<String> itr = vct.iterator();
        while(itr.hasNext()){
          System.out.println(itr.next());
Collections.sort(vct);

//display elements of Vector
System.out.println("Vector elements after sorting in ascending order : ");
for(int i=0; i<vct.size(); i++)
System.out.println(vct.get(i));
    }
Please log in to add an answer.