throw an exception in code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dlangdaman
    New Member
    • Jun 2007
    • 36

    throw an exception in code

    Its been a while since I have had Java..I am getting stuck tying to write a program that will add up grades and give and average...and throw an exception when a grade below 0 is added or above 100....can anyone spin me in the write directions?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Dlangdaman
    Its been a while since I have had Java..I am getting stuck tying to write a program that will add up grades and give and average...and throw an exception when a grade below 0 is added or above 100....can anyone spin me in the write directions?
    Are the grades going to be entered through the console?
    What do you have so far?

    Comment

    • Dlangdaman
      New Member
      • Jun 2007
      • 36

      #3
      im not sure..i would like them to be added through a small applet but I have no idea how to do that anymore...so if the console is the easiest way ....I have not yet started to actually put this together...i am looking for some direction on how to pull this off in the cleanest yet most basic way

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Dlangdaman
        im not sure..i would like them to be added through a small applet but I have no idea how to do that anymore...so if the console is the easiest way ....I have not yet started to actually put this together...i am looking for some direction on how to pull this off in the cleanest yet most basic way
        Most basic way is to do that through the console.
        Have a look at the java.util.Scann er class then try some code and post if you get any problems.

        Comment

        • Dlangdaman
          New Member
          • Jun 2007
          • 36

          #5
          here is what i have working....
          [CODE=java] import javax.swing.JOp tionPane;
          public class Grades
          {

          public Grades()
          {
          float grades [] = new float[5];
          float ave, sum = 0.0f;
          String students[] = {"Tim", "Mary", "Jen","Dere k", "John"};
          String sgrades, sResult;

          int i;
          for(i = 0; i < grades.length; ++i)
          {
          sgrades = JOptionPane.sho wInputDialog(nu ll,
          "Enter Students Grade: ", students[i],1);
          grades[i] = Float.parseFloa t(sgrades);
          }
          for(i = 0;i < grades.length; ++i)

          {
          sum += grades[i];
          }
          ave = sum / 5.0f;
          sResult = "The Class average is " + Float.toString( ave);
          JOptionPane.sho wMessageDialog( null,sResult);
          }
          public static void main(String args[])
          {
          new Grades();
          System.exit(0);
          }
          }
          [/CODE]

          what i need is an exception to happen if some one enters a grade above 100 or below zero...how would I do that?
          Last edited by r035198x; Jun 27 '07, 05:44 PM. Reason: added the missing code tags

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Dlangdaman
            here is what i have working....
            [CODE=java] import javax.swing.JOp tionPane;
            public class Grades
            {

            public Grades()
            {
            float grades [] = new float[5];
            float ave, sum = 0.0f;
            String students[] = {"Tim", "Mary", "Jen","Dere k", "John"};
            String sgrades, sResult;

            int i;
            for(i = 0; i < grades.length; ++i)
            {
            sgrades = JOptionPane.sho wInputDialog(nu ll,
            "Enter Students Grade: ", students[i],1);
            grades[i] = Float.parseFloa t(sgrades);
            }
            for(i = 0;i < grades.length; ++i)

            {
            sum += grades[i];
            }
            ave = sum / 5.0f;
            sResult = "The Class average is " + Float.toString( ave);
            JOptionPane.sho wMessageDialog( null,sResult);
            }
            public static void main(String args[])
            {
            new Grades();
            System.exit(0);
            }
            }
            [/CODE]

            what i need is an exception to happen if some one enters a grade above 100 or below zero...how would I do that?
            See how the code looks better under code tags? Don't forget them next time. Now there are many built in Exceptions in Java and you can make your own kinds of exceptions. So the next thing you want to do now is to create your own type of Exception. Then all you need is a simple if test that throws the exception if the grade is greater than 100. Have you read about Exceptions yet?

            Comment

            • Dlangdaman
              New Member
              • Jun 2007
              • 36

              #7
              Thank you for correcting the tags..i wasnt sure how to do that...as far as exceptions I am fairly new to them. i have been struggling on how or where to implement the exception in my code

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by Dlangdaman
                Thank you for correcting the tags..i wasnt sure how to do that...as far as exceptions I am fairly new to them. i have been struggling on how or where to implement the exception in my code
                Well the exceptions tutorial is fairly short. If you go through it you should be able to write this in no time. Do that and post back if you get problems.

                Comment

                • Dlangdaman
                  New Member
                  • Jun 2007
                  • 36

                  #9
                  Im sorry to bother you guys...I need someone to get me started..I cannot figure out how to imply an exception throw and catch into the code I already have...can anyone just show me where to insert? and first line

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Dlangdaman
                    Im sorry to bother you guys...I need someone to get me started..I cannot figure out how to imply an exception throw and catch into the code I already have...can anyone just show me where to insert? and first line
                    Did you finish that small Exceptions tutorial that I gave you?

                    Comment

                    • KishoreM
                      New Member
                      • Jun 2007
                      • 12

                      #11
                      Originally posted by Dlangdaman
                      Its been a while since I have had Java..I am getting stuck tying to write a program that will add up grades and give and average...and throw an exception when a grade below 0 is added or above 100....can anyone spin me in the write directions?
                      Hi,
                      please see the updated code with required exception handling mechanism.

                      <Removed>

                      regards,

                      Kishore M
                      Last edited by r035198x; Jun 28 '07, 09:29 AM. Reason: Please don't spoon feed

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by KishoreM
                        Hi,
                        please see the updated code with required exception handling mechanism.

                        <Removed>

                        regards,

                        Kishore M
                        Please read the site guidelines concerning posting full code solutions.

                        Comment

                        • Dlangdaman
                          New Member
                          • Jun 2007
                          • 36

                          #13
                          I apologize for making such a stink on the site. I did browse through the tutorial and I will do so again today. I was just wondering on how to place it in my code. Thanks again

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by Dlangdaman
                            I apologize for making such a stink on the site. I did browse through the tutorial and I will do so again today. I was just wondering on how to place it in my code. Thanks again
                            Don't worry, you didn't do much wrong. If there's any part of that tutorial that you don't understand you can post here and get help.

                            Comment

                            • Dlangdaman
                              New Member
                              • Jun 2007
                              • 36

                              #15
                              basically like I stated...my only issue is placement. I know that I must create a method that calls the exception. i need to make the exception and make it do what I need it to do in an if statement
                              try
                              if i>100 or i < 0
                              then
                              Throw
                              blah blah
                              catch?

                              is that the order or close?

                              Comment

                              Working...