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.

Page 2 of 2 FirstFirst 12
Results 26 to 29 of 29

Thread: Help with code

  1. #26
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with code

    Have you attempted to use a switch statement for your menu?

  2. The Following User Says Thank You to Bake1tEazy For This Useful Post:

    Hallowed (January 26th, 2011)

  3. #27
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with code

    and what is your overall code so far?

  4. #28
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Help with code

    I'm unfamiliar with how to use a switch statement.

    This is my overall code. It's working except for #2 there is some error in the way it searches through the array. I'm trying to find it. jk. i found it and fixed it
    package ArrayList;
     
    import java.util.Scanner;
     
    import java.util.ArrayList;
     
    public class Menu {
     
    	public static void main(String[] args) {
     
    		ArrayList<Student> studentRecords = new ArrayList<Student>(); // creates
    																		// array
    																		// list
     
    		System.out.println("Welcome!");
     
    		int UR = 0; // user Response 1
     
    		System.out.println("Please type the number of the task you would like to perform.");
    		System.out.println("1. Add a student");
    		System.out.println("2. Find a student");
    		System.out.println("3. Delete a student");
    		System.out.println("4. Display all students");
    		System.out.println("5. Display the total number of students");
    		System.out.println("6. Exit");
     
    		while (UR != 6) {
     
    			Scanner input = new Scanner(System.in); // ask for user response.
    			UR = input.nextInt(); // set UR equal to the users response.
     
    			if (UR == 1) {
    				System.out.println("Enter Firstname:> ");
    				String forename = "";
    				while (forename.isEmpty()) {
    					forename = input.nextLine();
    				}
     
    				System.out.println("Enter Surname:> ");
    				String surname = "";
    				while (surname.isEmpty()) {
    					surname = input.nextLine();
    				}
     
    				System.out.println("Enter Student Number:> ");
    				int studNo = -1;
    				while (studNo < 0) {
    					studNo = input.nextInt();
    				}
     
    				System.out.println("Enter Major:> ");
    				String studentMajor = "";
    				while (studentMajor.isEmpty()) {
    					studentMajor = input.nextLine();
    				}
     
    				System.out.println("Enter GPA:> ");
    				double gpa = -1.0;
    				while (gpa < 0) {
    					gpa = input.nextDouble();
    				}
     
    				Student newStudent = new Student(forename, surname, studNo,
    						studentMajor, gpa);
     
    				studentRecords.add(newStudent); // adds newStudent to the
    												// arraylist.
     
    				System.out.print("Success! ");
    				System.out.print(newStudent.getForeName());
    				System.out
    						.println("'s account has been created with the following information:"); // Print
    																									// verification.
    				System.out.println(newStudent);
    			}
     
    			if (UR == 2) {
    				int positionOfStudent = 0;
    				System.out.println("Enter a student name.");
    				String studentName = "";
     
    				while (studentName.isEmpty())
    				{
    				studentName = input.nextLine();
    				}
     
    				boolean found = false;
    				int i = 0;
    				while (found == false &&  positionOfStudent < studentRecords.size())
    				{
    				if (studentName.equalsIgnoreCase(studentRecords.get(i).getForeName()))
    				{
    				System.out.println("Student " + studentName + " found at position " + positionOfStudent + ".");
    				found = true;
    				}
     
    				else
    				{
    				positionOfStudent++;
                                    i++;
     
    				}
    				}
     
    				if (found == false)
    				System.out.println("Student not found.");
     
    				else
    				System.out.println(studentRecords.get(positionOfStudent).toString());
     
    				}
     
    			if (UR == 3) {
    				System.out.println("Enter the position of the student to remove.");
     
    				int position = input.nextInt();
     
    				if (position < 0 || position >= studentRecords.size())
    				{
    				System.out.println("Impossible");
    				}
     
    				else
    				{
    				studentRecords.remove(position);
    				}
     
     
    				}
     
    			if (UR == 4) {
    				for (Student s : studentRecords) {
    					System.out.println(s);
    				}
    			}
     
    			if (UR == 5) {
    				System.out.println("The total number of students is: " + studentRecords.size());
     
    			}
     
    			if (UR == 6) {
    				System.out.println("Good bye!");
    				System.exit(0);
    			}
    			System.out.println("Please select 1-6, to see the menu enter: 0");
    			if (UR == 0){
    				System.out.println("Please type the number of the task you would like to perform.");
    				System.out.println("1. Add a student");
    				System.out.println("2. Find a student");
    				System.out.println("3. Delete a student");
    				System.out.println("4. Display all students");
    				System.out.println("5. Display the total number of students");
    				System.out.println("6. Exit");
    			}
    		}
    	}
    }
    package ArrayList;
     
    public class Student {
     
        private String fName;
        private String lName;
        private int sNumber;
        private String major;
        private double gpa;
        static int count;
     
        public Student(String fName, String lName, int sNumber, String major, double gpa) {
            this.fName = fName;
            this.lName = lName;
            this.sNumber = sNumber;
            this.major = major;
            this.gpa = gpa;
     
            count++;//increment when object is created
        }
     
        //setter methods
        public void setForeName(String name) {
            fName = name;
        }
     
        public void setLastName(String lastName) {
            lName = lastName;
        }
     
        public void setStudentNumber(int sNo) {
            sNumber = sNo;
        }
     
        public void setMajor(String maj) {
            major = maj;
        }
     
        public void setGPA(double val) {
            gpa = val;
        }
     
        //getter methods - use to return values
        public String getForeName() {
            return fName;
        }
     
        public String getLastName() {
            return lName;
        }
     
        public int getStudentNumber() {
            return sNumber;
        }
     
        public String getMajor() {
            return major;
        }
     
        public double getGPA() {
            return gpa;
        }
     
        public static int getCount() {
            return count;
        }
     
        @Override
        public String toString() {
            return String.format("First name: %s, Last name: %s, Student "
                    + "Number: %d, Major: %s, GPA: %.2f", fName, lName, sNumber, major, gpa);
        } //called automatically when you attempt to print an object - System.out.println(myObject);
    }
    Last edited by Hallowed; January 26th, 2011 at 12:26 AM.

  5. #29
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Help with code

    Wow this all finally works and for the most part I understand it all.
    Thanks so much for everyone's help.
    ...Now I should probably rebuild this on my own sometime just to make sure I can. =P

Page 2 of 2 FirstFirst 12