Results 1 to 8 of 8
  1. #1
    sch518 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Question I need some help with my while loops and return statements

    Java Code:
    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    public class Gpa2 
    {
       public static void main(String[] args)
       {
          //DecimalFormat fmt = new DecimalFormat("0.00");
    	   
    	  Scanner scan = new Scanner(System.in);
          String stuName = "";
    	  String livingGroup = "";
    	  int numGroup = 0;
    	  double sumGpa= 0;
    	  double minGroupMain;
    	  double maxGroup = 0;
    	  double allMin = 10;
    	  double allMax = 0;
    	  double groupMin = 0;
    	  double groupMax = 0;
    	  double groupAverage = 0;
    	  double totalAverage = 0;
    	  final boolean DEBUG_SW = true;
    	  final String SENTINEL = "EndOfGroup";
    	  final int SIZE_ARR = 30;
    	  
    	  double[] gpa = new double[SIZE_ARR];	  	  
    	  
    	  while(scan.hasNextLine())
    	  {  
    		    			  	 
    		 while(!stuName.equals(SENTINEL))
    		 {
    	        livingGroup = scan.nextLine();	 
    		               
                System.out.println("Living Group:   " + livingGroup);     
    		 
                readGroup(gpa, SENTINEL, scan, SENTINEL, totalAverage, SIZE_ARR, DEBUG_SW);
             
                System.out.println("a");
             
                minGroup(gpa, SIZE_ARR);
             
                maxGroup(gpa, SIZE_ARR);
             
                sumGroup(gpa, SIZE_ARR);
             
    		    groupAverage = sumGpa / numGroup;
             
                System.out.println("Average GPA for this group = " + groupAverage);
                System.out.println("Minimum GPA for this group = " + minGroupMain);
                System.out.println("Maximum GPA for this group = " + maxGroup);
             
                if(groupMin > allMin)
                {
            	   allMin = groupMin; 
                }
             
                if(groupMax < allMax)
                {
            	   allMax = groupMax;
                }        
             
    		 }
    	     //scan.hasNextLine();
    	  }
    	  
    	  System.out.println("Minimum GPA of any student   = " + allMin);
    	  System.out.println("Maximum GPA of any student   = " + allMax);
       }
    
    public static double readGroup(double gpa[], String stuName, Scanner scan, String SENTINEL, double stuGpa, int SIZE_ARR, boolean DEBUG_SW)
       {
    	  int numGroup = 0;
    	  int i = 0;
    	   
          if(scan.hasNextInt() || scan.hasNextDouble())
          {
             System.out.println("Error must enter a first name.");
             System.exit(0);
          }
          
          stuName = scan.next();
          
          while(!stuName.equals(SENTINEL))
          {
             if(scan.hasNextDouble())
             {
                   gpa[i] = scan.nextDouble();
                  
                   numGroup++;
                   
                   System.out.println("   Student: " + stuName + " GPA = " + gpa[i]);
                                             
                   if(DEBUG_SW)
                   {
                      System.out.println("DEBUG");
                      System.out.println("   Current number of students read in is: " + numGroup + ".");
                   }         
              }
             
              if(scan.hasNextInt() || scan.hasNextDouble())
              {
                System.out.println("Error must enter a first name.");
                System.exit(0);
              }
              stuName = scan.next();
           }
          
          return numGroup;
       }
       
       public static double minGroup(double gpa[], int SIZE_ARR)
       {
          double minGroup = 0;
          
          minGroup = gpa[0];
          
          for(int i = 0; i < SIZE_ARR; i++)
          {
             if(minGroup > gpa[i])
             {
                minGroup = gpa[i];
             }
          }   
          return minGroup;
       }
       
       public static double maxGroup(double gpa[], int SIZE_ARR)
       {
          double maxGroup = 0;
          
          maxGroup = gpa[0];
          
          for(int i = 0; i < SIZE_ARR; i++)
          {
             if(maxGroup < gpa[i])
             {
                maxGroup = gpa[i];
             }
          }    
          return maxGroup;
       }
       
       public static double sumGroup(double gpa[], int SIZE_ARR)
       {
    	  double sumGpa = 0;
    	  
    	  for(int i = 0; i < SIZE_ARR; i++)
    	  {
             sumGpa = sumGpa + gpa[i];
    	  }
    	   
          return sumGpa;
       }
    
    }
    So this is my code for a homework assignment and I can't figure out what is wrong with it. I know it has something to do with the while loops in the main method. This is what I get when I run the program:

    in: Bennett Hall
    out: Living group: Bennett Hall
    in: Jim 3.0
    out: Student: Jim GPA = 3.0
    DEBUG
    Current number of students read in is: 1.
    in: Pam 2.9
    out: Student: Pam GPA = 2.9
    DEBUG
    Current number of students read in is: 2.
    in: EndOfGroup
    Average GPA for this group = NaN
    Minimum GPA for this group = 0.0
    Maximum GPA for this group = 0.0
    Living Group:


    When it gets to the end there it is expecting a name followed by a double for the GPA when I need it to read in the next line for a new living group. I think I just have the wrong order for the scan.next()'s in the while loops. I also do not know why the average GPA is given to be NaN and the min and max are both 0.0.

    Any help is appreciated. Thanks
    Last edited by sch518; 04-11-2011 at 08:13 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You call the methods but throw away the return values. This leaves your variables with the default value of 0.0.

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Good to see you took my advice and moved from Yahoo Answers to an actual Java forums.

  4. #4
    sch518 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    I have tried to research how to not throw away the return values but I am not getting anywhere. How do I save them instead of throwing them away?

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Could you place your code into [code] tags? That way the formatting sticks with the code. I'm trying to read through your code but its a pain since everything is all left justified.

  6. #6
    sch518 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Yes the left allign does make it hard to read. I am sorry that I am new to all this but I can't figure out how to post it like that.

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ok take your formatted code and place them inside a code tag. Your code tags will look like this
    Java Code:
    [co[b][/b]de]
    //My formatted code goes here
    [/co[b][/b]de]

  8. #8
    sch518 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    The while loops in the main method did not format correctly but hopefully this helps.

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  2. Return statements in methods
    By adjit in forum New To Java
    Replies: 12
    Last Post: 03-17-2011, 03:31 PM
  3. Replies: 9
    Last Post: 02-14-2011, 05:22 AM
  4. simple if statements and loops
    By merfen58 in forum New To Java
    Replies: 6
    Last Post: 09-22-2009, 10:46 AM
  5. Static Method and Return Statements
    By berelson in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 12:17 AM

Tags for this Thread

Posting Permissions

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