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

Thread: Letter grade isn't printing from the function to the output file...HELP!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Angry Letter grade isn't printing from the function to the output file...HELP!

     
    import java.io.*;
    import java.util.*;
    import java.lang.String;
    import java.lang.*;
     
    public class grades
    {
                public static double stuAve = 0.0;
                public static double classAve = 0.0;
                //public static  String grade = " ";
                public static void main (String[] args)
                                           throws FileNotFoundException
     
                                           {
                                           String name;
                                           int s1, s2, s3, s4, s5;
                                           double talley = 0.0;
                                           //double stuAve, classAve;
                                           String grade = " ";
     
     
                                           Scanner gradesData = new Scanner(new FileReader("F:\\gradesData.txt"));
                                           PrintWriter outfile = new PrintWriter("F:\\grades.txt");
     
                                          // classAve = 0.0;
     
     
                                   outfile.println(" Student   Test1  Test2  Test3  Test4  Test5  Average  Grade"); 
     
                                                while(gradesData.hasNext())
     
                                                {   name = gradesData.next();
                                                    outfile.printf("%s     ", name);
                                                    s1 = gradesData.nextInt();
                                                    outfile.printf("%-9d  " , s1);
                                                     s2 = gradesData.nextInt();
                                                    outfile.printf("%-7d  " , s2);
                                                    s3 = gradesData.nextInt();
                                                    outfile.printf("%-7d  " , s3);
                                                    s4 = gradesData.nextInt();
                                                    outfile.printf("%-7d  " , s4);
                                                    s5 = gradesData.nextInt();
                                                    outfile.printf("%-7d  " , s5);
     
                                                    calculateAverage(s1, s2, s3, s4, s5);
                                                    calculateGrade(stuAve);
     
                                                    outfile.printf("%.2f %n", stuAve);
                                                    outfile.print(grade);
     
                                                    talley= talley + stuAve;   }
     
                                                       classAve = talley /10.0;
     
                                                outfile.printf("Class Average = %.2f %n", classAve);
     
                                              gradesData.close();
                                              outfile.close();
                                       public static void calculateAverage(int s1,int  s2, int s3,int s4,int s5)
                                            {
                                stuAve = ((double) s1 + (double)s2 + (double)s3 + (double)s4 + (double)s5)/5.0;
                                                      }
                                                      public static String calculateGrade(double stuAve)
                                                       {
                                                              String grade = " ";
                                                              if(stuAve >= 93)
                                                               grade = "A";
                                                                else
                                                               if(stuAve >= 85 && stuAve <=92)
                                                               grade = "B";
                                                                else
    				                          if(stuAve >= 76 && stuAve <=84)
                                                              grade = "C";
                                                               else
    	                                                    if(stuAve >= 70 && stuAve <=75)
                                                               grade = "D";
                                                                else
                                                                 grade = "F";
                                                                // outfile.print(grade);
                                                                 return grade;
                                                               }  }




    Here is the output to the file:


    Student Test1 Test2 Test3 Test4 Test5 Average Grade
    Johnson 85 83 77 91 76 82.40
    Aniston 80 90 95 93 48 81.20
    Cooper 78 81 11 90 73 66.60
    Gupta 92 83 30 69 87 72.20
    Blair 23 45 96 38 59 52.20
    Clark 60 85 45 39 67 59.20
    Kennedy 77 31 52 74 83 63.40
    Bronson 93 94 89 77 97 90.00
    Sunny 79 85 28 93 82 73.40
    Smith 85 72 49 75 63 68.80
    Class Average = 70.94

    Formatting is always the last thing I do, so don't mind that...I have used both char and string to try and return the letter grade value from the calculateGrade function..I have tried making 'grade' a local, global, and regular variable of both the string and char types...still nothing...What gives???
    Attached Files Attached Files

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

    nrickinedi (November 24th, 2012)


  3. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Letter grade isn't printing from the function to the output file...HELP!

    Quote Originally Posted by 112297 View Post
    Formatting is always the last thing I do, so don't mind that...
    I'm going to ask you to fix this now. The reason you format your code is to make it easier for others to read and understand. Since you're asking volunteers to do just that, to read and understand your code, a process that takes effort and time, I don't think it's asking too much for you to put in a little effort and time to make it easier for us to do this. Thanks in advance.

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Letter grade isn't printing from the function to the output file...HELP!

    You mean the output or the actual code?

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Letter grade isn't printing from the function to the output file...HELP!

    Quote Originally Posted by 112297 View Post
    You mean the output or the actual code?
    Oh, the code, especially the indenting and use of whitespace. Indentations should be uniform and consistent. Please note that the forum software works best if you use spaces, not tabs, and often the best number of spaces for an intent is between 3 and 4.

  6. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Letter grade isn't printing from the function to the output file...HELP!

    I fixed it...is that better?

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Letter grade isn't printing from the function to the output file...HELP!

    I'll put the effort in for you this time since you're new, but this is what I mean by formatting:

    import java.io.*;
    import java.util.*;
    import java.lang.String;
     
    public class Grade {
       public static double stuAve = 0.0;
       public static double classAve = 0.0;
     
       // public static String grade = " ";
       public static void main(String[] args) throws FileNotFoundException {
          String name;
          int s1, s2, s3, s4, s5;
          double talley = 0.0;
          // double stuAve, classAve;
          String grade = " ";
          Scanner gradesData = new Scanner(new FileReader("F:\\gradesData.txt"));
          PrintWriter outfile = new PrintWriter("F:\\grades.txt");
          // classAve = 0.0;
          outfile.println(" Student     Test1     Test2     Test3     Test4     Test5     Average  Grade   ");
          while (gradesData.hasNext()) {
             name = gradesData.next();
             outfile.printf("%s     ", name);
             s1 = gradesData.nextInt();
             outfile.printf("%-9d  ", s1);
             s2 = gradesData.nextInt();
             outfile.printf("%-7d  ", s2);
             s3 = gradesData.nextInt();
             outfile.printf("%-7d  ", s3);
             s4 = gradesData.nextInt();
             outfile.printf("%-7d  ", s4);
             s5 = gradesData.nextInt();
             outfile.printf("%-7d  ", s5);
             calculateAverage(s1, s2, s3, s4, s5);
             calculateGrade(stuAve);
             outfile.printf("%.2f %n", stuAve);
             outfile.print(grade);
             talley = talley + stuAve;
          }
          classAve = talley / 10.0;
          outfile.printf("Class Average = %.2f %n", classAve);
          gradesData.close();
          outfile.close();
       }
     
       public static void calculateAverage(int s1, int s2, int s3, int s4, int s5) {
          stuAve = ((double) s1 + (double) s2 + (double) s3 + (double) s4 + (double) s5) / 5.0;
       }
     
       public static String calculateGrade(double stuAve) {
          String grade = " ";
          if (stuAve >= 93)
             grade = "A";
          else if (stuAve >= 85 && stuAve <= 92)
             grade = "B";
          else if (stuAve >= 76 && stuAve <= 84)
             grade = "C";
          else if (stuAve >= 70 && stuAve <= 75)
             grade = "D";
          else
             grade = "F";
          // outfile.print(grade);
          return grade;
       }
    }

    For most of us code that is formatted well is much easier to read and to understand.

    Now seeing this code, I can see what your problem is. You call the calculateGrade method here:

          while (gradesData.hasNext()) {
             name = gradesData.next();
             outfile.printf("%s     ", name);
             // .... 
     
             calculateGrade(stuAve);
             outfile.printf("%.2f %n", stuAve);
             outfile.print(grade);
             talley = talley + stuAve;
          }

    But you never assign what is returned by the method into the grade variable, and so it remains holding a simple space, " ".

    To fix this, simply assign the result returned by this method:

          while (gradesData.hasNext()) {
             name = gradesData.next();
             outfile.printf("%s     ", name);
             // .... 
     
             grade = calculateGrade(stuAve);  // ****** here *****
             outfile.printf("%.2f %n", stuAve);
             outfile.print(grade);
             talley = talley + stuAve;
          }

    Note the difference?

  8. The Following User Says Thank You to curmudgeon For This Useful Post:

    112297 (November 22nd, 2012)

  9. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Letter grade isn't printing from the function to the output file...HELP!

    Really? I thought that's what I was doing inside of the function...So all I needed to do was assign grade the value of the function? Thank you so much Curmu! I'm going to try it now and let you know what happens...:-)

    --- Update ---

    It worked! Thank you! ...Now the fun part, formatting the output! lol

  10. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Letter grade isn't printing from the function to the output file...HELP!

    Glad you've got it working!

    --- Update ---

    I think that the key to understanding why it didn't work before, is that the variable, grade, located within the calculateGrade method is not the same grade variable in the main method. The first one is local to the calculateGrade method and thus exists only inside of this method.

Similar Threads

  1. Return letter grade to main method
    By jmanv888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 15th, 2012, 04:48 PM
  2. [SOLVED] Output isn't as expected.
    By woodcutterni in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 11:55 AM
  3. [SOLVED] why does this code print 0.0 for the value and "F" as the letter grade?
    By etidd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 12th, 2010, 10:38 PM
  4. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 09:14 PM
  5. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM