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

Thread: Java Program help cleanup

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Program help cleanup

    The code here is working fine but the problem lies with the if else statements. Not that they do not work they do. But having said that I would like to clean it up some. When I implemented the if else statement it was to distinguish between Graduate and Undergrad. I would like to consolidate some of this code by removing repeated code. Both of the Graduate and Undergrad are extended classes from a Student class which is abstract.
    Any thoughts on this would be appreciated.

     
    <if (choice == 1) {
                        do {//do while block
                            try {//try catch block for exception handeling
                                // collect grades of tests
                                for (int i = 0; i < noTests; i++) {
                                    System.out.printf("Enter grade number for %s %s %d: ", grad.getFirstname(), grad.getLastname(), i + 1);
                                    grad.setGrades(scan.nextInt());                           
                                flag = false;
                                }
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loop
                        do {//do while block
                            try {//try catch block for exception handeling
                                System.out.printf("Enter Midterm grade for: %s %s ", grad.getFirstname(), grad.getLastname());
                                grad.setmidtermgrad(scan.nextInt());
                                flag = false;
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loop
                        do {
                            try {//try catch block for exception handeling
                                System.out.printf("Enter final grade for: %s %s ", grad.getFirstname(), grad.getLastname());
                                grad.setfinalgrade(scan.nextInt());
                                flag = false;
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loopp                  
                        //collect information and display results
                        System.out.printf("Average grade for: %s %s %.2f\n", grad.getFirstname(), grad.getLastname(), grad.getGradeAvg());
                        System.out.printf("THE FINAL GRADE FOR STUDENT: %s %s %.2f\n", grad.getFirstname(), grad.getLastname(), grad.getFinalGrade());
                    } else if (choice == 2) {//begin else statement
                        do {//do while block
                            try {//try catch block for exception handeling
                                // collect grades of tests
                                for (int i = 0; i < noTests; i++) {
                                    System.out.printf("Enter grade number %d: ", i + 1);
                                    Grad.setGrades(scan.nextInt());
                                }
                                flag = false;
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loop
                        do {
                            try {//try catch block for exception handeling
                                System.out.printf("Enter final grade for: %s %s ", Grad.getFirstname(), Grad.getLastname());
                                Grad.setfingrade(scan.nextInt());
                                flag = false;
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loop
                        System.out.printf("Average grade for: %s %s %.2f\n", Grad.getFirstname(), Grad.getLastname(), Grad.getGradeAvg());
                        System.out.printf("THE FINAL GRADE FOR STUDENT: %s %s %.2f\n", Grad.getFirstname(), Grad.getLastname(), Grad.getFinGrade());
                    }>


  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: Java Program help cleanup

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Program help cleanup

    thanks I did that

  4. #4
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Java Program help cleanup

    If you use these code multiple times.

     try {//try catch block for exception handeling
                                System.out.printf("Enter Midterm grade for: %s %s ", grad.getFirstname(), grad.getLastname());
                                grad.setmidtermgrad(scan.nextInt());
                                flag = false;
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }

    You might be able to store those in a subclass.
    And then you only need for example: subclass() to execute that code.
    That saves some lines, and when you need to change something in that part you only have to do it one time and not multiple times.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program help cleanup

    okay but I was thinking more about the Graduate and Undergrad choices and responses.
    how can i get with the user choice of Graduate or Undergrad response to be coincided.

    example for both Graduate and Undergrad the question of number of tests and the question of midterm and final grade without repeating those questions?

Similar Threads

  1. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  2. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM
  3. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 12:25 PM
  4. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  5. Dice game help, a little cleanup
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 12:28 PM

Tags for this Thread