DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Problem with Strings

  1. #1
    Join Date
    Mar 2004
    Posts
    2

    Problem with Strings

    import cs1.Keyboard;

    public class StudentGrades
    {
    //**********************************************************
    // Reads the Input from the User and Prints Data
    //**********************************************************
    public static void main(String[] args)
    {
    String familyName;
    String firstName;
    String firstName2;
    String subject1;
    int grade1;
    String finalGrade1;
    String subject2;
    int grade2;
    String finalGrade2;
    String subject3;
    int grade3;
    String finalGrade3;


    System.out.println("Student Grades program");
    System.out.println("****************************");

    System.out.print("Enter Student family name: ");
    familyName = Keyboard.readString();

    System.out.print("Enter Student first name: ");
    firstName = Keyboard.readString();

    System.out.print("Enter Subject: ");
    subject1 = Keyboard.readString();
    System.out.print("Enter Mark (0-100): ");
    grade1 = Keyboard.readInt();

    System.out.print("Enter Subject: ");
    subject2 = Keyboard.readString();
    System.out.print("Enter Mark (0-100): ");
    grade2 = Keyboard.readInt();

    System.out.print("Enter Subject: ");
    subject3 = Keyboard.readString();
    System.out.print("Enter Mark (0-100): ");
    grade3 = Keyboard.readInt();

    System.out.print("Results for ");

    firstName2 = firstName.substring(0,1);
    System.out.println(firstName2 + "." +familyName );

    if(grade1 <=49)
    finalGrade1="fail";

    else if (50 <= grade1 && grade1 <= 59)
    finalGrade1="Pass";

    else if (60 <= grade1 && grade1 <=69)
    finalGrade1="Credit";

    else if(70 <= grade1 && grade1<=79)
    finalGrade1="Distinction";

    else if(80 <= grade1 && grade1<=100)
    finalGrade1= "High Distinction";


    System.out.print(subject1 + " " + finalGrade1);
    }
    }
    Ok why isnt it showing the finalGrade1, what am i missing here
    Anyadvice would be helpful
    PS First Post yay

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    Did you even get that code to compile? I compiled it and got the error "Variable finalGrade1 might not have been initialised". This means that there is a path through the program that would result in finalGrade1 not being assigned a value, and as such the System.out.println() statement at the end would fail due to a null pointer exception. To resolve the problem add an else statement to the end and assign something to the variable finalGrade1. This will mean that there isn't a path through the program that will result in finalGrade1 being null. I added
    Code:
    else
    {
        finalGrade1 = "incorrect input";
    }
    and it worked fine.

  3. #3
    Join Date
    Feb 2004
    Posts
    808

    Re: Problem with Strings

    Originally posted by token
    Ok why isnt it showing the finalGrade1, what am i missing here
    Anyadvice would be helpful
    PS First Post yay
    hang on a sec here.. read your IF clauses:

    if grade is less than 50
    otherwise is grade is less than 51 and it is also less than 61
    otherwise if the grade is less than 61 and it is less than 71

    do you see what i mean? there are redundant clauses in there..
    if an item is less than 51 it sure as heck is going to be less than 61

    you can modify your clauses, you dont need anywhere near that many:

    if item is less than 40
    else if it is less than 50
    else if it is less than 60


    an item at 45 wont be caught by the first clause.. it will be caught by the second.. the remaining clasuses are never checked anyway (thats what elseif is all about)
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Mar 2004
    Posts
    2
    Yeah i get your point but what im saying is this:

    else if (50 <= grade1 && grade1 <= 59)
    this means:
    else if grade1 is greater then or equal to 50 and
    grade1 is less then or equal to 59 then activate code

    Thanks heaps
    mikeBarr81 that was such a simple problem hey, your a frikin champ

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    you're welcome but what Cjard said is a valid point. You only need to test the equality once in each if statement.

    if (grade < 50)
    catch here

    else if(grade < 60)
    catch here

    else if(grade < 70) catch here

    etc

    you don't need to check that the grade is greater than 50 and less than 60, you only need to check that it is less than 60. If the first if statement doesn't catch it, then you already know that the variable is >= 50, so you don't need to check it again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center