So am fairly new to programming in general , and i wanted to make a simple student database system, using array lists and methods , thing is i have a problem with my add_inf() method where it ignores my first name.add(in.nextLine); and doesnt take any input for it and goes straight to the second command here is the full code:
package advanced.db;
import java.util.ArrayList;
import java.util.Scanner;
public class AdvancedDb {
static Scanner in = new Scanner(System.in);
static ArrayList<String> st_name = new ArrayList<>();
static ArrayList<String> age = new ArrayList<>();
static ArrayList<String> address = new ArrayList<>();
public static void add_info(){
System.out.print("enter Name :");
st_name.add(in.nextLine());
System.out.print("enter age :");
age.add(in.nextLine());
System.out.print("enter address :");
address.add(in.nextLine());
}
public static void view() {
for (int x = 0; x < st_name.size() && x < age.size() && x < address.size(); x++) {
System.out.println(st_name.get(x));
System.out.println(age.get(x));
System.out.println(address.get(x) + "\t");
}
if (st_name.isEmpty() && age.isEmpty() && address.isEmpty()) {
System.out.println("list is empty");
}
}
public static void delete() {
System.out.println("please enter the name you want to be deleted");
String input;
input = in.nextLine();
for (int x = 0; x < st_name.size() && x < age.size() && x < address.size(); x++) {
if (input.equals(st_name.get(x))) {
System.out.println("info of the name (" + input + ")\n is deleted");
st_name.remove(x);
age.remove(x);
address.remove(x);
}
}
}
public static void search() {
System.out.println("enter the name or age you're searching for ");
String input;
input = in.nextLine();
for (int x = 0; x < st_name.size() && x < age.size() && x < address.size(); x++) {
if (input.equals(st_name.get(x))) {
System.out.println("INFO regarding " + input + "is found");
System.out.println("name :" + st_name.get(x));
System.out.println("age :" + age.get(x));
System.out.println("address :" + address.get(x));
} else if (input.equals(age.get(x))) {
System.out.println("students with the age (" + input + ")");
System.out.println("name :" + st_name.get(x));
System.out.println("age :" + age.get(x));
System.out.println("address :" + address.get(x) + "\t");
} else {
System.out.println("nothing is found regarding the input");
}
}
if (st_name.isEmpty() && age.isEmpty() && address.isEmpty()) {
System.out.println("list is empty");
}
}
public static void main(String[] args) {
int x = 0;
System.out.println("welcome\n\n");
System.out.println("what do you want to do ");
System.out.println("1-view the lsit of students ");
System.out.println("2-add a new student");
System.out.println("3-delete a student ");
System.out.println("4-search for student ");
System.out.println("5-exit \n");
while (x == 0) {
System.out.print("choice :");
switch (in.nextInt()) {
case 1:
view();
break;
case 2:
add_info();
break;
case 3:
delete();
break;
case 4:
search();
break;
case 5:
x = 1;
System.out.println("exiting now");
break;
default:
System.out.println("incorrect input try again");
break;
}
}
}
}
for some reason the output when you press 2 as your choice is name:age: address: taking age and address only i tried removing the name.add(in.nextLine) and left age and address it did the same thing ignoring age and taking address only i would really appreciate it if someone helped me understand why this is happening