1. #1

    Programming Help

    I need to make a program that calculates the GPA of students. It needs to use multiple methods and I am getting snagged.

    public static char grade(Scanner in, String name) {
    char grade;
    System.out.println("Please enter the grade received in " + name + ": ");
    grade = in.next().toUpperCase().charAt(0);

    while ((grade != 'A' )|| (grade != 'B') || (grade != 'C') || (grade != 'D') || (grade != 'F') || (grade != 'X')) {

    System.out.println("Please re-enter a valid letter grade: ");
    grade = in.next().toUpperCase().charAt(0);


    }
    return grade;


    At the point of the while statement, it keeps asking me to re-enter a grade, even when I use A, B , C and so on. Any suggestions?


    Edit: Never mind, I think I found my error was using or instead of and.
    Last edited by Potatopancake; 2012-10-25 at 08:56 PM. Reason: Found my error.

  2. #2
    Change ||s with &&s will be ok, reason:

    if he enters G:

    (true && true && true && true && true) = true --> stays in while

    if he enters A:

    (false && true && true && true && true) = false --> leaves while
    Last edited by Marooned; 2012-10-25 at 09:01 PM.

  3. #3
    You know it'd be a lot easier if you used arrays or lists.

    Code:
    private static char[] _grades = {'A','B','C','D','F','X'};
    
    function()
    {
        while(!_grades.asList().contains(ch))
        ....
    }

  4. #4
    The Patient
    10+ Year Old Account
    Join Date
    Nov 2010
    Location
    New Jersey
    Posts
    312
    Quote Originally Posted by Grinderofl View Post
    You know it'd be a lot easier if you used arrays or lists.

    Code:
    private static char[] _grades = {'A','B','C','D','F','X'};
    
    function()
    {
        while(!_grades.asList().contains(ch))
        ....
    }
    hes probably in a intro programming class that hasnt gotten there yet?

    but onto this
    while ((grade != 'A' )|| (grade != 'B') || (grade != 'C') || (grade != 'D') || (grade != 'F') || (grade != 'X')) {

    System.out.println("Please re-enter a valid letter grade: ");
    grade = in.next().toUpperCase().charAt(0);


    }
    id do something along these lines

    Code:
    if((grade != 'A') && (grade != 'B') && (grade != 'C') && (grade != 'D') && (grade != 'F'))
    {
    System.out.println("Not a valid grade, please enter a valid grade");
    Scanner sc = new Scanner(System.in);
    grade = sc.next().toUpper();
    while ((grade != 'A') && (grade != 'B') && (grade != 'C') && (grade != 'D') && (grade != 'F'))
    {
    System.out.println("Not a valid grade, please enter a valid grade");
    Scanner sc = new Scanner(System.in);
    grade = sc.next().toUpper();
    }
    }
    else
    grade = "whatever you named your scanner for reading user input";
    that should about right, im pretty sure

  5. #5
    A Do-while construct would be better in this case, as it guarantees that the body of the loop will be executed at least once, i.e.

    Code:
    do
    {
        System.out.println("Enter a grade:");
        Scanner sc = new Scanner(System.in);
        grade = sc.next().toUpper();
    }while ((grade != 'A') && (grade != 'B') && (grade != 'C') && (grade != 'D') && (grade != 'F'));
    By doing this you remove the if-else block and the associated code. Realistically it is a matter of personal preference, but I find the do-while construct more readable then the embedded if / while construct, but your mileage may vary.

    If you want, you can get fancy and change the text that is output base on if is the first or subsequent times through the loop. Also you can embed the above construct into another loop (probably a while loop) to allow input of multiple grades.
    Last edited by thurizas; 2012-10-25 at 11:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •