Results 1 to 11 of 11
  1. #1
    yrvd86 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default not sure what im doing wrong with these if else statements

    I am having problems with this. this is supposed to give you a list of things. the full code is this

    Java Code:
    package students; 
    import javax.swing.JOptionPane;
    public class Students
    {
    
    public static void main(String[] args) 
      {
    
    
    //first list the integers and strings which are
    //the student’s  major (CIS or Math)
    //The student’s last name
    //The number of credits the student has completed
    //The gpa
    //The tuition paid
    
    
    
        int tuition, finish = 1, students=0, credits, major, credits1 = 0, credits2 = 0, credits3 = 0, credits4 = 0;
        String smajor, last, sgpa, stuition, sfinish, scredits;
        double largestg = 0, largestm1 = 0, largestm2 = 0, gpa, totalgpa=0, totaltuition=0;
        String highnameg = "", highnamem1 = "", highnamem2 = "";
        
        int totalcredits=0;
    
        while (finish != 0) 
        {
    students=students+1;
          smajor = JOptionPane.showInputDialog(null,
                                           "Enter students major (1 for math 2 for cis)", "Input Data",
                                              JOptionPane.QUESTION_MESSAGE);
    
          last = JOptionPane.showInputDialog(null,
                                             "Enter students last name",
                                             "Input Data",
                                             JOptionPane.QUESTION_MESSAGE);
    
          scredits = JOptionPane.showInputDialog(null,
              "Enter students total amount of credits", "Input Data",
              JOptionPane.QUESTION_MESSAGE);
    
          sgpa = JOptionPane.showInputDialog(null,
                                             "Enter students GPA", "Input Data",
                                             JOptionPane.QUESTION_MESSAGE);
    
          stuition = JOptionPane.showInputDialog(null,
                                                 "Enter students tuition paid",
                                                 "Input Data",
                                                 JOptionPane.QUESTION_MESSAGE);
    
          credits = Integer.parseInt(scredits);
          gpa = Double.parseDouble(sgpa);
          tuition = Integer.parseInt(stuition);
          major = Integer.parseInt(smajor);
          
          totalgpa=totalgpa+gpa;
          totaltuition=totaltuition+tuition;
          totalcredits=totalcredits+credits;
          if (gpa > largestg) 
          {
            largestg = gpa;
            highnameg = last;
          }
         
          if (major == 1)
          {
        	 if (gpa > largestm1)
        	 {
        	 largestm1 = gpa;
             highnamem1 = last;
        	  
        	 }
        	 
        	 else if (major == 2)
        	 {
        		 
        		 if (gpa > largestm2)
            	 {
            	 largestm2 = gpa;
                 highnamem2 = last;
            	 }  
            	 
          }
         
          if (credits <= 30)
          {
        	  credits1 = students;
          }
          else if (credits >=31 && credits <=60)
          {
        	  credits2 = students;
          }
          else if (credits >=61 && credits <=90)
          {
        	  credits3 = students;    	
          }
          else if (credits >=90)
          {
        	  credits4 = students;
          }
          }
          
          
          sfinish = JOptionPane.showInputDialog(null,
              "Enter 0 if your done or 1 to continue", "Input Data",
              JOptionPane.QUESTION_MESSAGE);
          finish = Integer.parseInt(sfinish);
          
        } //while loop
    //do calcs and such
        double agpa = totalgpa / students;
        double avgtu = totaltuition / students;
        int avgcr = totalcredits / students;
    
    //print out results
        JOptionPane.showMessageDialog(null,
                                      "\nAverage GPA = " + agpa +
                                      "\nTuition = $" + totaltuition +
                                      "\nAverage tuition = $" + avgtu +
                                      "\nAverage credits = $" + avgcr +
                                      "\nHighest GPA from " + highnameg +" " +largestg +
                                      "\nHighest GPA from a math student is " + highnamem1 +" from " +largestm1+ 
                                      "\nHighest GPA from a cis student is " + highnamem2 +" from " +largestm2+
                                      "\nNumber of Freshman is " +credits1+ 
                                      "\nNumber of Sophomore is " +credits2+ 
                                      "\nNumber of Junior is " +credits3+ 
                                      "\nNumber of Senior is " +credits4+ 
                                       JOptionPane.INFORMATION_MESSAGE);
      }
    
    
    }
    for some reason this statement does not work properly. the first part works but then the second part does not
    Java Code:
    if (major == 1)
    {
    if (gpa > largestm1)
    {
    largestm1 = gpa;
    highnamem1 = last;
    
    }
    
    else if (major == 2)
    {
    
    if (gpa > largestm2)
    {
    largestm2 = gpa;
    highnamem2 = last;
    } 
    
    }
    ..........
    
     JOptionPane.showMessageDialog(null,
                                      "\nAverage GPA = " + agpa +
                                      "\nTuition = $" + totaltuition +
                                      "\nAverage tuition = $" + avgtu +
                                      "\nAverage credits = $" + avgcr +
                                      "\nHighest GPA from " + highnameg +" " +largestg +
                                      "\nHighest GPA from a math student is " + highnamem1 +" from " +largestm1+ 
                                      "\nHighest GPA from a cis student is " + highnamem2 +" from " +largestm2+

    and the next one wont work at all

    Java Code:
    if (credits <= 30)
    {
    credits1 = students;
    }
    else if (credits >=31 && credits <=60)
    {
    credits2 = students;
    }
    else if (credits >=61 && credits <=90)
    {
    credits3 = students; 
    }
    else if (credits >=90)
    {
    credits4 = students;
    }
    }
    
    .......
    
    
     "\nNumber of Freshman is " +credits1+ 
                                      "\nNumber of Sophomore is " +credits2+ 
                                      "\nNumber of Junior is " +credits3+ 
                                      "\nNumber of Senior is " +credits4+ 
                                       JOptionPane.INFORMATION_MESSAGE);
    I appreciate any help you can give
    Last edited by yrvd86; 04-27-2010 at 01:14 AM. Reason: done incorrectly

  2. #2
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default Welcome to the Forum

    please try to make it easier for someone to help you

    first you should edit your post and repaste your code using code tags
    see: Java Forums - BB Code List

    exactly why is the code not working? state what it should return and state what it is actually returning
    see: How To Ask Questions The Smart Way
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  3. #3
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    i cant see any problems, although I hasten to add i'm not an expert.]
    there are some spacing indentation and coding style issues that make your code difficult to read.
    perhaps your editor can fix the spacing and indentation for you
    if you use Eclipse 'select all' (ctrl a)
    then on the 'source' dropdown menu there are 'format' command and 'correct indentation' command.

    you say the code does not work but you don't say why exactly.

    what does the code do wrong. what are you expecting to get in the final list compared with what are you actually getting.
    Last edited by sonny; 04-27-2010 at 02:09 AM.
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  4. #4
    yrvd86 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    depending on if you type one or two for major it should show how many students have that major plus their gpa. The first one works which would be this

    Java Code:
    if (major == 1) {
    				if (gpa > largestm1) {
    					largestm1 = gpa;
    					highnamem1 = last;
    
    				}
    but then the next part for 2 will not show any results in the end

    Java Code:
    else if (major == 2) {
    
    					if (gpa > largestm2) {
    						largestm2 = gpa;
    						highnamem2 = last;
    					}
    
    				}
    Then the statments for the credits wont show the results for freshman through senior

    Java Code:
    if (credits <= 30) {
    					credits1 = students;
    				} else if (credits >= 31 && credits <= 60) {
    					credits2 = students;
    				} else if (credits >= 61 && credits <= 90) {
    					credits3 = students;
    				} else if (credits >= 90) {
    					credits4 = students;
    				}
    			}
    This doesnt really make sense to me since the first one works fine. im not sure if there is a continuity error or what but it will not work. Thanks again

  5. #5
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    the reason why it is so important to indent and space code correctly is so that you quckly find the things that are wrong

    take a look at your curly braces...

    does the if else ever get evaluated to be true?

    Java Code:
                  if (major == 1) [COLOR="Red"]{[/COLOR]
    			if (gpa > largestm1) [COLOR="Red"]{[/COLOR]
    				largestm1 = gpa;
    				highnamem1 = last;
    
    			[COLOR="Red"]}[/COLOR]
    
    			else if (major == 2) {
    
    				if (gpa > largestm2) {
    					largestm2 = gpa;
    					highnamem2 = last;
    				}
    
    			}
    
    			if (credits <= 30) {
    				credits1 = students;
    			} else if (credits >= 31 && credits <= 60) {
    				credits2 = students;
    			} else if (credits >= 61 && credits <= 90) {
    				credits3 = students;
    			} else if (credits >= 90) {
    				credits4 = students;
    			}
    		}
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  6. #6
    yrvd86 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    So then what should I change on it? I honestly dont see the error

  7. #7
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Lightbulb

    okay your code says this
    the colour changes as your blocks of code change

    the code before this is green
    if major == 1
    open curly brace
    do this next block of code

    open curly brace
    if gpa > ...... etc
    close curly brace....

    so were done with the purple block and it is closed so we back with the blue block

    then it says if major == 2,, how can major be == 2, were are still in the blue block.
    the blue block of code is executed because major == 1, if major == 1 it cant == 2

    count your curly braces, how many have you opened, how many have you closed??

    does that make sense. post straight back if not
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  8. #8
    yrvd86 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks alot I really appreciate your help it worked.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This confuses me too:
    Java Code:
    			if (credits <= 30) {
    				credits1 = students;
    			} else if (credits >= 31 && credits <= 60) {
    				credits2 = students;
    			} else if (credits >= 61 && credits <= 90) {
    				credits3 = students;
    			} else if (credits >= 90) {
    				credits4 = students;
    			}
    		}
    I'm guessing that here if the student has less then 30 credits, increase the count of freshmen by 1 (credits1), else if they have 31 to 60 credits, increase the number of sophomores (credits2) by 1, etc...

    If so, you are gaining nothing by setting credits1 or 2 or 3 or 4 to the total number of students at that time as this logically doesn't make sense. More logical (in my limited understanding of your problem) is to instead increment credits1 or credits2 or credits3 or credits3 depending on the value of credits. In other words, something like:
    Java Code:
    			if (credits <= 30) {
    				credits1++;
    			} else if (credits >= 31 && credits <= 60) {
    				credits2++;
    			} else if (credits >= 61 && credits <= 90) {
    				credits3++;
    			} else if (credits >= 90) {
    				credits4++;
    			}
    		}
    If I'm totally missing your problem, then please ignore this recommendation, but please let me know.

  10. #10
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    you still have another curly brace issue to solve to get your code to work
    and it is here

    Java Code:
    else if (credits >= 90) {
    					credits4 = totalStudents;
    				}
    			[COLOR="Red"]}[/COLOR]
    look at your whole while loop,, how many have you opened, and how many have you closed???

    which block of code is controlling things.. (possibly not the best way of explaining it)

    i think you need to remove the red curly brace and it all should work,:D


    EDIT hadnt seen fubarables post, and was still just looking at curly braces
    Last edited by sonny; 04-27-2010 at 03:50 AM.
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  11. #11
    yrvd86 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks alot im gonna try out incrementing it. I appreciate all of your help

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 07:02 PM
  3. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 09:50 PM
  4. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 04:24 PM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 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
  •