Develop a Java program to add student’s names and display it using vectorclass.
Question
Develop a Java program to add student’s names and display it using Vector
class.
Solution
Here is a simple Java program to add student names using the Vector
class and display them:
import java.util.Vector;
import java.util.Scanner;
public class StudentNames {
public static void main(String[] args) {
// Step 1: Create a Vector to store student names
Vector<String> studentNames = new Vector<String>();
// Step 2: Initialize Scanner for user input
Scanner scanner = new Scanner(System.in);
// Step 3: Add student names to the Vector
System.out.println("Enter student names (type 'exit' to stop):");
while (true) {
String name = scanner.nextLine();
if (name.equalsIgnoreCase("exit")) {
break; // Exit the loop if 'exit' is entered
}
studentNames.add(name); // Add the name to the Vector
}
// Step 4: Display the names stored in the Vector
System.out.println("Student Names:");
for (String name : studentNames) {
System.out.println(name);
}
// Close the scanner
scanner.close();
}
}
Explanation of the Program
- Initialization: The program imports necessary classes and initializes a
Vector
to hold the student names. - Input Loop: It uses a
Scanner
to read names from the user repeatedly until the user types "exit". - Adding Names: Names are added to the
Vector
with theadd()
method. - Display: Finally, it iterates through the
Vector
and prints each name.
Final Note
To run this program, ensure you have a Java development environment set up. Compile and execute the program, then follow the prompts to add student names. You can type "exit" when you are done entering names.
Similar Questions
Generate a list of the Instructor names by combining "Professor " and the first name, 1 space, and last name togethe
Write a program that displays the total students in each section.For example:Input ResultStudents.csvSectionA 2B 2C 3Name: StudentName, dtype: int64
Java Program To Accept the Marks of a Student and Find the Total Marks and Percentage
using the while loop, and scenner draft a java code that grades students when they enter makes it grades it with A,B+,B,C,C+ and F grades
ind a solution identify duplicate entries in the student name list of size 1000 using Top downdesign approach?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.