Results 1 to 12 of 12
  1. #1
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default error in if statement

    Hello again oh guru's of the java world. I'm having some problems here, certain it's minor but I can't figure it out. I've loooked tried and nothing works.

    I have a piece of code and i'm trying to calculate a grade letter, everything work however the final if prints letter 'D' when it should print letter 'C'. Attached is the code, hoping someone can see what I can't.


    import java.util.Scanner;

    //this class will call Mili
    public class IndividTrainee{

    /**********
    *Section 2*
    **********/
    //declare global variables
    static int tNum, quiznum;
    static float quizmark, totalmark=0.0f, finmark, averagemark, x=0.2f, y=0.8f, overallmark;
    static String famName, firstN, insmodule, letter, lgrade;
    static boolean trainnum=false, familyname=false, firstname=false, instructionmodule=false;
    static boolean quiznumber=false, mark=false, finalmark=false;

    /**********
    *Section 3*
    **********/
    public static void main(String []args){

    //declare instance of Scanner
    Scanner in=new Scanner(System.in);

    /**********
    *Section 4*
    **********/
    do{
    System.out.print("Trainee Number: ");
    String traineenumber=in.nextLine();
    tNum=Integer.parseInt(traineenumber);
    trainnum=isTraineeNum(tNum);
    }//close traineenumber do loop
    while(!trainnum);

    do{
    System.out.print("Family Name: ");
    famName=in.nextLine();
    familyname=isFamName(famName.trim());
    }//close familyname loop
    while(!familyname);

    do{
    System.out.print("First Name: ");
    firstN=in.nextLine();
    firstname=isFirstName(firstN.trim());
    }//close firstname loop
    while(!firstname);

    do{
    System.out.print("Instruction Module Name: ");
    insmodule=in.nextLine();
    instructionmodule=isInsModule(insmodule.trim());
    }//close instruction module loop
    while(!instructionmodule);

    do{
    System.out.print("How many quizes shall I calculate: ");
    String iquiznum=in.nextLine();
    quiznum=Integer.parseInt(iquiznum);
    quiznumber=isQuizNum(quiznum);
    }//close quiz amount loop
    while(!quiznumber);

    /**********
    *Section 5*
    **********/
    for(int i=1;i<=quiznum;i++){
    do{
    System.out.print("Module Quiz "+i+": ");
    String iquizmark=in.nextLine();
    quizmark=Float.parseFloat(iquizmark);
    mark=isQuizMark(quizmark);
    }//close do loop
    while(!mark);
    totalmark=totalmark+quizmark;
    }//close for loop

    do{
    System.out.print("Final Module Mark: ");
    String ifinalmark=in.nextLine();
    finmark=Float.parseFloat(ifinalmark);
    finalmark=isFinalMark(finmark);
    }
    while(!finalmark);

    Military myMilitary=new Military(tNum, famName, firstN, insmodule, GradeLetter(letter));
    System.out.println();
    System.out.println("Trainee Number: "+myMilitary.getTraineeNum());
    System.out.println("Trainee Name: "+myMilitary.getFirstName()+" "+myMilitary.getFamilyName());
    System.out.println("Instruction module name: "+myMilitary.getInstructionModule());
    System.out.println("Average Mark: "+AverageMark(totalmark, quiznum)+" Final Module Mark: "+finmark+" Overall Mark: "+OverallMark(averagemark, finmark));
    System.out.println(myMilitary.getLetterGrade());

    }//close main

    /**********
    *Section 6*
    **********/
    public static boolean isTraineeNum(int tNum){
    /**********
    *Section 7*
    **********/
    if(tNum>999 && tNum<10000){
    return true;
    }else{
    System.out.println("The number you entered is out of range, please try again?");
    return false;
    }//close if-else statement
    }//close isTraineeNum method

    public static boolean isFamName(String famName){
    if(famName.length()==0||famName==("")){
    System.out.println("Please enter your family name correctly?");
    return false;
    }else{
    return true;
    }//close if-else statement
    }//close isFamName

    public static boolean isFirstName(String firstN){
    if(firstN.length()==0||firstN==("")){
    System.out.println("Please enter your first name correctly?");
    return false;
    }else{
    return true;
    }//close if-else statement
    }//close isFamName

    public static boolean isInsModule(String insmodule){
    if(insmodule.length()==0||insmodule==("")){
    System.out.println("Please enter your instruction module name correctly?");
    return false;
    }else{
    return true;
    }//close if-else statement
    }//close isFamName

    public static boolean isQuizNum(int quiznum){
    if(quiznum>0 && quiznum<9){
    return true;
    }else{
    System.out.println("Please enter a value within the range of 1 to 8?");
    return false;
    }//close if-else statement
    }//close isQuizNumber

    public static boolean isQuizMark(float mark){
    if(mark>0.0f && mark<=100.0f){
    return true;
    }else{
    System.out.println("Please enter a grade mark between 0 and 100?");
    return false;
    }//close if else statement
    }//close isQuizMark

    public static boolean isFinalMark(float finmark){
    if(finmark>=0.0f && finmark<=100.0f){
    return true;
    }else{
    System.out.println("Please enter a final grade mark between 0 and 100?");
    return false;
    }//close if-else statement
    }//close isFinalMark

    public static float AverageMark(float a, int b){
    averagemark=a/b;
    return averagemark;
    }//close AverageMark method

    public static float OverallMark(float averagemark, float finmark){
    overallmark=averagemark*x+finmark*y;
    return overallmark;
    }//close OverallMark method

    public static String Grade(float overallmark){
    if(overallmark >=35.0f && overallmark<=39.9f){
    letter="Letter Grade: E";
    }else{ if(overallmark>=40.0f && overallmark<=49.9f){
    letter="Letter Grade: D";
    }else{ if(overallmark>=50.00f && overallmark<=59.90f){
    letter="Letter Grade: C";
    }else{ if(overallmark>=60.0f && overallmark<=69.9f){
    letter="Letter Grade: B";
    }else{ if(overallmark >=70.0f){
    letter="Letter Grade: A";
    }else{
    letter="I'm sorry, but you FAILED.";
    }//close else
    }
    }
    }
    }
    return letter;
    }//close Grade method

    public static String GradeLetter(String lgrade){
    lgrade=Grade(OverallMark(averagemark,finmark));
    return lgrade;
    }//close GradeLetter method


    }//close class

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    When posting code, it should be in the form of an SSCCE. If your problem is with a single method, post some runnable code that demonstrates that single method. Boil it down to a single problem: is the Grade() method (methods should start with a lowercase letter, by the way) returning the wrong value? Are you sure you're passing in the correct parameter?

    And don't forget the code tags.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i just made some tests with the method Grade() but i couldn't find any error. please do what KevinWorkman suggest.

  4. #4
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default tried everything and still doesn't work

    I tried it and still nothing. However a few classmates of mine has the same code and there's work fine just different variable names.

    I'm attaching the class file that goes with the object so you can see and hopefully see my dilemma.

    /**********
    *Section 8*
    ***********/
    /*
    ***Program Name: IndividTrainee.java
    ***Created Date: Dec, 15th, 2010
    ***Created By: Kirk Ali
    ***Date Modified: Dec, 15th, 2010
    ***Description: This program takes individual user input and prints out in detail information
    calculating both the user overall mark along with a grade letter.
    ***Methods Used: getTraineeNum(),
    getFirstName(),
    getFamilyName(),
    getInstructionModule(),
    getLetterGrade()
    */
    public class MilitaryOrg{
    public String traineeFirstName;
    public String traineeLastName;
    public int traineeNumber;
    public String instructionModule;
    public String letterGrade;

    public MilitaryOrg(int TraineeNumber, String TraineeFirstName, String TraineeLastName, String InstructionModule, String LetterGrade){
    this.traineeNumber=TraineeNumber;
    this.traineeFirstName=TraineeFirstName;
    this.traineeLastName=TraineeLastName;
    this.instructionModule=InstructionModule;
    this.letterGrade=LetterGrade;
    }//closes Mil

    public int getTraineeNum(){
    return this.traineeNumber;
    }//closes getTraineeNumber

    public String getFirstName(){
    return this.traineeFirstName;
    }//closes getFirstName

    public String getFamilyName(){
    return this.traineeLastName;
    }//closes getFirstName

    public String getInstructionModule(){
    return this.instructionModule;
    }//closes method

    public String getLetterGrade(){
    return this.letterGrade;
    }//closes method

    }//closes Mil





    I appreciate all the help, hoping you all can find what I missed.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    No. You didn't narrow the problem down to an SSCCE, and you didn't even use code tags. You can't just dump code and expect somebody to do your debugging for you. Sorry.

  6. #6
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    You're right, I do apologise and I wasn't trying to get anyone to do it for me. I simplified and found the problem at the method,

    public static String GradeLetter(String lgrade){
    lgrade=Grade(OverallMark(averagemark,finmark));
    return lgrade;
    }//close GradeLetter method

    when i ran the code without this method everything worked fine. So I know the issue is in here problem is my classmate and I can't figure it out. we tried every which way we know as beginner programmers and nothing comes to mind.

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    This method doesn't seem to make any sense. Whatever string is passed into the method is not taken into account, it is simply overwritten by the Grade() method and then returned. Why do you even need that method if you're just returning what Grade() gives you?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  8. #8
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    I believe the program is supposed to work like setters and getters where we created and instantiation of the militaryorg file, calculate a score, turn it into a lettergrade and send it to the militaryorg file. then pull it out back out in the statement myMilitaryOrg.getLetterGrade() method. However, all the other return statements work cept for this. This afternoon I had a student presently in year three and another in year 2 of the bachelor of science program in computer and information system and they themselves couldn't figure it out.

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    code tags please, it has been requested, there are many posters here who have the courtesy to post code tags and make there code readable to programmers, I would love to help you, but I refuse on the grounds you have not put your code in tags, and it has been requested before me....


    to put code in tags
    [code]

    //code here

    [/cod] but with an 'e' after 'd' obviously to make the word code
    Last edited by al_Marshy_1981; 12-16-2010 at 12:59 AM. Reason: the word in has not become the word int no matter how much you program lol

  10. #10
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Is this better.....

    public class MilitaryOrg{
    public String traineeFirstName;
    public String traineeLastName;
    public int traineeNumber;
    public String instructionModule;
    public String letterGrade;

    public MilitaryOrg(int TraineeNumber, String TraineeFirstName, String TraineeLastName, String InstructionModule, String LetterGrade){
    this.traineeNumber=TraineeNumber;
    this.traineeFirstName=TraineeFirstName;
    this.traineeLastName=TraineeLastName;
    this.instructionModule=InstructionModule;
    this.letterGrade=LetterGrade;
    }//closes Mil

    public int getTraineeNum(){
    return this.traineeNumber;
    }//closes getTraineeNumber

    public String getFirstName(){
    return this.traineeFirstName;
    }//closes getFirstName

    public String getFamilyName(){
    return this.traineeLastName;
    }//closes getFirstName

    public String getInstructionModule(){
    return this.instructionModule;
    }//closes method

    public String getLetterGrade(){
    return this.letterGrade;
    }//closes method

    }//closes Mil




    END OF CLASS FILE.
    NEXT FILE IS OBJECT FILE.

    public class IndividTrainee{

    /**********
    *Section 2*
    **********/
    //declare global variables
    static int tNum, quiznum;
    static float quizmark, totalmark=0.0f, finmark, averagemark, x=0.2f, y=0.8f, overallmark;
    static String famName, firstN, insmodule, letter, lgrade;
    static boolean trainnum=false, familyname=false, firstname=false, instructionmodule=false;
    static boolean quiznumber=false, mark=false, finalmark=false;

    /**********
    *Section 3*
    **********/
    public static void main(String []args){

    //declare instance of Scanner
    Scanner in=new Scanner(System.in);

    /**********
    *Section 4*
    **********/
    do{
    System.out.print("Trainee Number: ");
    String traineenumber=in.nextLine();
    tNum=Integer.parseInt(traineenumber);
    trainnum=isTraineeNum(tNum);
    }//close traineenumber do loop
    while(!trainnum);

    do{
    System.out.print("Family Name: ");
    famName=in.nextLine();
    familyname=isFamName(famName.trim());
    }//close familyname loop
    while(!familyname);

    do{
    System.out.print("First Name: ");
    firstN=in.nextLine();
    firstname=isFirstName(firstN.trim());
    }//close firstname loop
    while(!firstname);

    do{
    System.out.print("Instruction Module Name: ");
    insmodule=in.nextLine();
    instructionmodule=isInsModule(insmodule.trim());
    }//close instruction module loop
    while(!instructionmodule);

    do{
    System.out.print("How many quizes shall I calculate: ");
    String iquiznum=in.nextLine();
    quiznum=Integer.parseInt(iquiznum);
    quiznumber=isQuizNum(quiznum);
    }//close quiz amount loop
    while(!quiznumber);

    /**********
    *Section 5*
    **********/
    for(int i=1;i<=quiznum;i++){
    do{
    System.out.print("Module Quiz "+i+": ");
    String iquizmark=in.nextLine();
    quizmark=Float.parseFloat(iquizmark);
    mark=isQuizMark(quizmark);
    }//close do loop
    while(!mark);
    totalmark=totalmark+quizmark;
    }//close for loop

    do{
    System.out.print("Final Module Mark: ");
    String ifinalmark=in.nextLine();
    finmark=Float.parseFloat(ifinalmark);
    finalmark=isFinalMark(finmark);
    }
    while(!finalmark);

    Military myMilitary=new Military(tNum, famName, firstN, insmodule, GradeLetter(letter));
    System.out.println();
    System.out.println("Trainee Number: "+myMilitary.getTraineeNum());
    System.out.println("Trainee Name: "+myMilitary.getFirstName()+" "+myMilitary.getFamilyName());
    System.out.println("Instruction module name: "+myMilitary.getInstructionModule());
    System.out.println("Average Mark: "+AverageMark(totalmark, quiznum)+" Final Module Mark: "+finmark+" Overall Mark: "+OverallMark(averagemark, finmark));
    System.out.println(myMilitary.getLetterGrade()); [this method prints d (incorrect) when you input two test scores 10 and 30 with the final entry as 61]
    System.out.println(GradeLetter()); [this method prints c when you input two test scores 10 and 30 with the final as 61]

    }//close main

    /**********
    *Section 6*
    **********/
    public static boolean isTraineeNum(int tNum){
    /**********
    *Section 7*
    **********/
    if(tNum>999 && tNum<10000){
    return true;
    }else{
    System.out.println("The number you entered is out of range, please try again?");
    return false;
    }//close if-else statement
    }//close isTraineeNum method

    public static boolean isFamName(String famName){
    if(famName.length()==0||famName==("")){
    System.out.println("Please enter your family name correctly?");
    return false;
    }else{
    return true;
    }//close if-else statement
    }//close isFamName

    public static boolean isFirstName(String firstN){
    if(firstN.length()==0||firstN==("")){
    System.out.println("Please enter your first name correctly?");
    return false;
    }else{
    return true;
    }//close if-else statement
    }//close isFamName

    public static boolean isInsModule(String insmodule){
    if(insmodule.length()==0||insmodule==("")){
    System.out.println("Please enter your instruction module name correctly?");
    return false;
    }else{
    return true;
    }//close if-else statement
    }//close isFamName

    public static boolean isQuizNum(int quiznum){
    if(quiznum>0 && quiznum<9){
    return true;
    }else{
    System.out.println("Please enter a value within the range of 1 to 8?");
    return false;
    }//close if-else statement
    }//close isQuizNumber

    public static boolean isQuizMark(float mark){
    if(mark>0.0f && mark<=100.0f){
    return true;
    }else{
    System.out.println("Please enter a grade mark between 0 and 100?");
    return false;
    }//close if else statement
    }//close isQuizMark

    public static boolean isFinalMark(float finmark){
    if(finmark>=0.0f && finmark<=100.0f){
    return true;
    }else{
    System.out.println("Please enter a final grade mark between 0 and 100?");
    return false;
    }//close if-else statement
    }//close isFinalMark

    public static float AverageMark(float a, int b){
    averagemark=a/b;
    return averagemark;
    }//close AverageMark method

    public static float OverallMark(float averagemark, float finmark){
    overallmark=averagemark*x+finmark*y;
    return overallmark;
    }//close OverallMark method

    public static String Grade(float overallmark){
    if(overallmark >=35.0f && overallmark<=39.9f){
    letter="Letter Grade: E";
    }else{ if(overallmark>=40.0f && overallmark<=49.9f){
    letter="Letter Grade: D";
    }else{ if(overallmark>=50.00f && overallmark<=59.90f){
    letter="Letter Grade: C";
    }else{ if(overallmark>=60.0f && overallmark<=69.9f){
    letter="Letter Grade: B";
    }else{ if(overallmark >=70.0f){
    letter="Letter Grade: A";
    }else{
    letter="I'm sorry, but you FAILED.";
    }//close else
    return letter;
    }//close Grade method

    public static String GradeLetter(){
    lgrade=Grade(OverallMark(averagemark,finmark));
    return lgrade;
    }//close GradeLetter method


    }//close class



    THE METHODS WORK AS EXPECTED, HOWEVER WHEN YOU COMPILE AND RUN YOU ARE ASKED TO INPUT TWO TEST SCORES I USED 10 AND 30 THEN ASKED FINAL TEST SCORE AS 61. AFTER THE PROGRAM CALCULATES AND PRINTS FINAL, IT PRINTS 'D' USING THE INSTANTIATION METHOD WHERE IT SENDS THE INFORMATION TO THE MIL FILE AND RETURNS. HOWEVER, IF YOU USE THE LAST METHOD WITHOUT SENDING THE INFORMATION AS IN THE FINAL PRINT STATEMENT AT THE END OF MAIN PRINT, IT PRINTS 'C' WHICH IS CORRECT. I FIGURED OUT THIS PART TONIGHT, IT'S COMING SLOWLY BUT I CAN'T REACH PAST THIS PART NOW. HOPE YOU ALL CAN UNDERSTAND, IF NOT I'LL TRY AND CLEAR IT AS BEST AS I CAN. BUT I'M STILL NEW TO THIS FORUM SO FORGIVE ME, OTHERWISE I CAN SIMPLY ATTACH MY NOTEPAD++ FILE FOR YOU ALL TO TAKE A LOOK AT, NOT SURE HOW TO GO ABOUT DOING THAT.


    THANKS IN ADVANCE, PLEASE BE LENIENT......:o

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    still no code tags.....

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Missing return statement error.
    By Fortu in forum New To Java
    Replies: 2
    Last Post: 12-11-2010, 09:15 PM
  2. Strange unreachable statement error...
    By silafirion in forum New To Java
    Replies: 5
    Last Post: 12-11-2010, 05:05 AM
  3. Return statement error
    By Exothesis in forum New To Java
    Replies: 2
    Last Post: 10-13-2010, 01:56 AM
  4. Missing Return Statement Error
    By darkblue24 in forum New To Java
    Replies: 13
    Last Post: 02-16-2010, 08:22 PM
  5. Which statement is throwing a runtime error....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 12:55 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
  •