Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Arraylist input help, quick question.

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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.
    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;
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arraylist input help, quick question.

    its not working.
    Can you explain?
    If you are getting errors, copy and paste the error message here.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

    but that keeps giving an error
    Please post the full text of the error message if you want help with it.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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;

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arraylist input help, quick question.

    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.

Similar Threads

  1. Quick question
    By l1nk3 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2012, 02:02 PM
  2. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  3. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  4. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  5. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM