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

Thread: not sure what im doing wrong with these if else statements

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    for some reason the if else statements related to major and credits wont work correctly. I get the first if statement for major to work but the second does not.

    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);
      }
     
     
    }

    any help is appreciated thank you


  2. #2
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

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

    Quote Originally Posted by yrvd86 View Post
    for some reason the if else statements related to major and credits wont work correctly. I get the first if statement for major to work but the second does not.

    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);
      }
     
     
    }

    any help is appreciated thank you
    hi. welcome to the JPF.

    Which ones not working? Can you try to print it so you can easily debug it? maybe it doestnt satisfy the condition.

Similar Threads

  1. continue statements
    By monroe in forum Java Applets
    Replies: 1
    Last Post: March 20th, 2010, 06:26 PM
  2. Unreachable statements
    By Marcus in forum Loops & Control Statements
    Replies: 7
    Last Post: December 10th, 2009, 02:02 PM
  3. need help with JButton and switch statements
    By jjoubert in forum AWT / Java Swing
    Replies: 5
    Last Post: October 28th, 2009, 09:13 AM
  4. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM
  5. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 02:15 AM