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 4 of 4

Thread: need help fast!!

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help fast!!

    hey i need to make a program where a menu pops up. the user will enter a number and the program will continue doing whatever they have selected. so if they hit 1 they can add a student to the arraylist. 2 is suppose to delete a student after the user enters and id number. 3 shows size fo arraylist. 4 prints out the name and id of each student on arraylist. 5 finds a student with id. i need help on 2, 4 , and 5. please can someone tell me how to print the name and id of a student while pulling it from arraylist.

    import java.util.*;
     
    public class NollisP3A{
    	public static void main (String[] args){
     
    	///student kid = new student(id, name);
    	ArrayList studentList = new ArrayList();
     
    	String menu= "Enter 1 to: Add a student to the list.\nEnter 2 to: Delete a student from the list.\nEnter 3 to: Display number of students in the list.\nEnter 4 to: Display information (ID and name) for all students.\nEnter 5 to. Find a student with a specific ID.\nEnter 0 to: Quit the program.";
     
     
    	System.out.println(menu);	
    	Scanner input1 = new Scanner(System.in);
    	int choice = input1.nextInt();
    	int n = 0;
     
    	if (choice == 0){
    		System.out.println("Program Ended");
    		System.exit(0);
    			}//end if
    		else{
    			while(choice!= 0){
    			switch (choice){
    				case 1: System.out.println("Enter student ID");
     
    						Scanner idInput = new Scanner(System.in);
    						int id = idInput.nextInt();
    						System.out.println("Enter student name");
    						Scanner nameInput = new Scanner(System.in);
    						String name = nameInput.next();
    						student kid = new student(id,name);
    						studentList.add(n++, kid);
    						System.out.print(kid.id +"\n\n");
     
    						System.out.println(menu);	
    						choice = input1.nextInt();
    					break;
     
     
    				case 2: System.out.println("Enter ID number to delete the student");
     
    					Scanner delete = new Scanner(System.in);
    				//	kid.id = delete.nextInt();
    				//	studentList.remove(kid.id);
     
    					System.out.println(menu);	
    					choice = input1.nextInt();
    					break;
     
     
    				case 3: System.out.println("Number of Students: " + studentList.size());
     
    						System.out.println(menu);	
    						choice = input1.nextInt();
    					break;
     
     
    				case 4:
    					//display students and id
    					int i =0;
    					while(i < studentList.size()){
    					//System.out.println(((student)studentList.get(i).getName()));
    					i++;
     
    					}
     
     
     
    					System.out.println(menu);	
    					choice = input1.nextInt();
    					break;
     
     
    				case 5: System.out.println("Enter the ID number.");
    					Scanner findId = new Scanner(System.in);
     
    				//System.out.println(kid.id);
     
    					System.out.println(menu);	
    					choice = input1.nextInt();
    					break;
     
     
    				default: System.out.println("Error: Try again.");
    					System.out.println(menu);	
    					choice = input1.nextInt();		
     
    				}//end switch
    			}//end while
    		}//end else
    		getName(){
     
    		}
    	}//end main
    }//end of nollis
     
    class student{
    	 int id;
    	 String name;
     
     
     
    	student(){
    		}
     
    	student(int newId, String newName){
    		id=0;
    		name= null;
     
    		getId();
    		getName();
    		id = newId;
    		name= newName;
    	}
     
    	int getId(){
    		return this.id;
    	}
     
     
    	String getName(){
    	return this.name;
    	}
    }
    Last edited by helloworld922; September 27th, 2010 at 05:54 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: need help fast!!

    how to print the name and id of a student while pulling it from arraylist.
    Use the get method to "pull" an element/object from the arraylist.

    Then extract the name and id of the student from the object and print them.

    Your code has the statement that prints the name commented out???

    Please enclose your code in code tags to preserve formatting. Info here: Java Forums - BB Code List

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help fast!!

    i was just going back through and trying fix stuff, thats why they were commented out. im not sure about the pull method could you give an example. also i am having trouble with the accesor and mutator methods. i have to have name and id private in student class.

    import java.util.*;
     
    public class NollisP3A{
    	public static void main (String[] args){
     
     
    	ArrayList studentList = new ArrayList();
     
    	String menu= "Enter 1 to: Add a student to the list.\nEnter 2 to: Delete a student from the list.\nEnter 3 to: Display number of students in the list.\nEnter 4 to: Display information (ID and name) for all students.\nEnter 5 to. Find a student with a specific ID.\nEnter 0 to: Quit the program.";
     
     
    	System.out.println(menu);	
    	Scanner input1 = new Scanner(System.in);
    	int choice = input1.nextInt();
    	int n = 0;
     
    	if (choice == 0){
    		System.out.println("Program Ended");
    		System.exit(0);
    			}//end if
    		else{
    			while(choice!= 0){
    			switch (choice){
    				case 1: System.out.println("Enter student ID");
     
    						Scanner idInput = new Scanner(System.in);
    						int id = idInput.nextInt();
    						System.out.println("Enter student name");
    						Scanner nameInput = new Scanner(System.in);
    						String name = nameInput.next();
    						student kid = new student(id,name);
    						studentList.add(n++, kid1);
    						System.out.print(kid.id +"\n\n");
     
    						System.out.println(menu);	
    						choice = input1.nextInt();
    					break;
     
     
    				case 2: System.out.println("Enter ID number to delete the student");
    					Scanner delete = new Scanner(System.in);
    					int del = delete.nextInt();
    					studentList.remove(kid.id == del);
     
    					System.out.println(menu);	
    					choice = input1.nextInt();
    					break;
     
     
    				case 3: System.out.println("Number of Students: " + studentList.size());
     
    						System.out.println(menu);	
    						choice = input1.nextInt();
    					break;
     
     
    				case 4:
    					//display students and id
    					int i =0;
    					while(i < studentList.size()){
    					System.out.println(((student)studentList.get(i).getName()));
    					i++;
     
    					}
     
     
     
    					System.out.println(menu);	
    					choice = input1.nextInt();
    					break;
     
     
    				case 5: System.out.println("Enter the ID number.");
    					Scanner findId = new Scanner(System.in);
     
    					System.out.println(kid.id);
     
    					System.out.println(menu);	
    					choice = input1.nextInt();
    					break;
     
     
    				default: System.out.println("Error: Try again.");
    					System.out.println(menu);	
    					choice = input1.nextInt();		
     
    				}//end switch
    			}//end while
    		}//end else
    	}//end main
    }//end of nollis
     
    class student{
    	 private int id;
    	 private String name;
     
     
     
    	student(){
    		}
     
    	student(int newId, String newName){
    		getId();
    		getName();
    		id = newId;
    		name= newName;
     
    	}
     
    	int getId(){
    		return this.id;
    	}
     
     
    	String getName(){
    	return this.name;
    	}
    }

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,630
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: need help fast!!

    The API is your best friend in JAVA.

    To find the API for any Java Standard Object, just Google: java+[object]+6 and it is usually the first link.

    For example, we want to get the ArrayList API. So we Google: java+ArrayList+6. The first link is: ArrayList (Java Platform SE 6)

    Perfect. Now we want to find a method that does what we want. Based on method descriptions, this is what we want:
    get
    public E get(int index)Returns the element at the specified position in this list.

    Specified by:
    get in interface List<E>
    Specified by:
    get in class AbstractList<E>
    Parameters:
    index - index of the element to return
    Returns:
    the element at the specified position in this list
    Throws:
    IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

    Remember, if you are looking for something specific, always check the API before asking around for help and waiting for people to respond. 99% of the time, checking the API is MUCH faster.

Similar Threads

  1. Externally sorting ints as fast as possible
    By Djikstrangle in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 21st, 2010, 03:00 PM
  2. Code stopping, need help fast.
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2010, 09:00 AM

Tags for this Thread