Question
I'm trying to add elements to my array list but every time i try to add a new elements it overwrites the previous data. when
I'm trying to add elements to my array list but every time i try to add a new elements it overwrites the previous data. when i add two or more Students the more recent data replaces all the previous data instead of keeping the previous data. so if i have three students: Robert, Jake , and Mike when i "View Students" all the elements will be Mike, Mike and Mike. Loc8.java
import java.util.*; public class Loc8 { static int count = 0; static int input = 0; static ArrayList
menuOptions(in); } public static void menuOptions(Scanner in) {
System.out.println("Welcome To Loc8! ----------------"); System.out.println("(1) Create Student"); System.out.println("(2) Edit Student"); System.out.println("(3) Delete Student"); System.out.println("(4) View Students"); System.out.println("(5) Search For Student"); System.out.println("(6) Exit"); System.out.print("Choose an Option: ");
if(!in.hasNextInt()) { in.next(); } else input = in.nextInt(); switch(input) { case 1: student.add(Create_Student.student()); count++; System.out.println(count); break; case 2:
System.out.println(count); break; case 3: System.out.println("Delete"); break; case 4: System.out.println(Arrays.toString(student.toArray())); break; case 5: System.out.println("Search"); break; case 6: System.exit(0); default: System.out.println("Input is Invalid! Try Again."); break; } menuOptions(in); } }
Student.java
public class Student { static String firstName = ""; static String lastName = ""; static String grade = ""; static String major = ""; static String race = ""; static int age = 0; public Student(String firstName, String lastName, String grade, String major, String race, int age) { this.firstName = firstName; this.lastName = lastName; this.grade = grade; this.major = major; this.race = race; this.age = age; } public static String studentInfo() { System.out.print(" Student Information "); System.out.println("------------------- "); System.out.printf("First Name: %7s ", firstName); System.out.printf("Last Name: %8s ", lastName); System.out.printf("Grade: %12s ", grade); System.out.printf("Major: %12s ", major); System.out.printf("Race: %13s ", race); System.out.printf("Age: %14s", age);
return String.format(" "); } //@Override public String toString() { return firstName + " " +lastName; } }
Create_Student.java
import java.util.*; public class Create_Student { public static void main(String []args) { } public static Student student() { Scanner in = new Scanner(System.in);
System.out.print("Enter 1st Name: "); String firstName = in.next(); System.out.print("Enter last Name: "); String lastName = in.next(); System.out.print("Enter Grade Classification: "); String grade = in.next(); System.out.print("Enter Major: "); String major = in.next(); System.out.print("Enter Race: "); String race = in.next(); System.out.print("Enter Age: "); int age = in.nextInt();
Student student = new Student(firstName, lastName, grade, major, race, age);
return student; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started