Arraylist input help, quick question.
Hi, Im pretty new to java, and this is my first time using Arraylist. Im having trouble having storing the users input to the arraylist, i know i have to use add. .. but its not working. Any help would be thank full. Ill post my appointment class, and my other class that im using to get the users input.
//This is my appointment class, basicly has date, time, and owners name of a appointment.
Code :
public class Appointment implements Comparable<Appointment> {
private String date;
private String time;
Owner owner;
public Appointment(String date, String time, Owner owner) {
this.date = date;
this.time = time;
this.owner = owner;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) {
this.owner = owner;
}
public String toString() {
return "(Owner name: " + owner.getName() + ", Date: " + date + ", Time: " + time + ")";
}
// return number < 0 if THIS appointment comes before 'a'. Returns > 0 if comes AFTER. 0 if same date
public int compareTo(Appointment a) {
return getDate().compareTo(a.getDate());
}
}
// This class basicly is the main menu, and it should get the users input and store it in the appointment arraylist.
//this class isnt done, but if i can get case 5 to work i should be fine with the rest of the cases.
Code :
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author Sarmen
*
*/
public class Menu {
public static int MenuSelection() {
Scanner kb = new Scanner(System.in);
int selection = 0;
ArrayList<Appointment> appts = new ArrayList<Appointment>();
ArrayList<Animal> animals = new ArrayList<Animal>();
ClientDataBase db = new ClientDataBase(appts, animals);
while (true) {
System.out.println("Welcome to Sarmen's Veterinarian office");
System.out.println("What would you like to do? ");
System.out.println("[1]. See all appointments");
System.out.println("[2]. Search by Date");
System.out.println("[3]. Search by Owner");
System.out.println("[4]. Search by Animal(Dog/Fish/Bird)");
System.out.println("[5]. Add appointment");
System.out.println("[6]. Exit");
selection = kb.nextInt();
switch (selection) {
case 1:
System.out.println("All appointments");
db.printAllAppointments();
break;
case 2:
System.out.println("Date of appointment to search: ");
break;
case 3:
System.out.println("Owners name to search: ");
break;
case 4:
System.out.println("Enter(Dog/Fish/Bird): ");
break;
case 5:
System.out.println("Adding Appointment");
kb.nextLine();
System.out.println("Enter Date(mm/dd/yy): ");
appts.add(new Appointment(null ,null, null)); //i think im not supposed to have all the nulls
kb.nextLine();
System.out.println("Enter time(hh:mm am/pm)");
appts.add(null);
kb.nextLine();
System.out.println("Enter Owner name)");
appts.add(null);
kb.nextLine();
db.printAllAppointments();
break;
case 6:
System.out.println("Exit");
System.exit(0);
default:
System.out.println("Please enter a valid selection.");
}
return selection;
}
}
}
Re: Arraylist input help, quick question.
Can you explain?
If you are getting errors, copy and paste the error message here.
Re: Arraylist input help, quick question.
I think where i have case 5, i have to have something like new Appointments(date, time, name), but that keeps giving an error saying create field, create parameter, and etc..., basically i need help adding the users input of clients name, date of appointment, and time of appointment, in to the appointment array
Exception in thread "main" java.lang.NullPointerException
at edu.csupomona.cs.cs141.Vet.Appointment.toString(Ap pointment.java:50)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at edu.csupomona.cs.cs141.Vet.Menu.MenuSelection(Menu .java:67)
Re: Arraylist input help, quick question.
There is a variable with a null value that the code is trying to use at line 50.
Look at line 50 and find the variable with the null value and then backtrack in the code to see why that variable does not have a valid, non null value.
Quote:
but that keeps giving an error
Please post the full text of the error message if you want help with it.
Re: Arraylist input help, quick question.
That is the full error message i posted
//im doing this case 5 wrong, i need something to get the users input of time, date, and owner, and put it in to ArrayList<Appointment> that i have
Code :
case 5:System.out.println("Adding Appointment");
kb.nextLine();
System.out.println("Enter Date(mm/dd/yy): ");
appts.add(new Appointment(null ,null, null)); //i think im not supposed to have all the nulls
kb.nextLine();
System.out.println("Enter time(hh:mm am/pm)");
appts.add(null);
kb.nextLine();
System.out.println("Enter Owner name)");
appts.add(null);
kb.nextLine();
db.printAllAppointments();
break;
Re: Arraylist input help, quick question.
Quote:
i need something to get the users input
The Scanner class has methods for getting input from a user. Your code has kb set be able to read from the console.
You have already used it once here:
selection = kb.nextInt();
NOTICE that there is a variable to the left of the = where the value returned by the method is stored.
Now you need to use Scanner class methods to get the rest of the data from the user.
Once you get all the data, create an instance of the class and add it to the arraylist.