1

This is a Constructor that I have for a test average class. My assignment asks me to bring in an array-list of test score and as input validation, it wants me to use a try catch statement to catch any input under 0 and over 100.

The constructor below is bringing in an array-list from my main with out any error. However, it is not catching a negative input. I been looking at this code for over two hours and I can't figure it out. I thought a fresh pair of eyes could probably see why it is not catching bad input.

My whole Program:

class:

import javax.swing.*;
import java.util.*;


class try2
{
    public static ArrayList<Integer>userInput=new ArrayList<Integer>();
    public static double avg;

public try2()
{
}

public try2(ArrayList<Integer> test) 
{
  for ( int x = 0 ; x <= test.size(); x++)
  {
      try
      { 
        if ( test.get(x) < 0 || test.get(x) > 100) 
        {
            throw new IllegalArgumentException ();
        } 
        else
        {
            this.userInput = test;  
        }
     } 
    catch ( IllegalArgumentException ex) {
    JOptionPane.showMessageDialog(null," NO NEGETIVES ALLOWED ");
    }
  }   
}

public static void setAvg ()
{
        int sum = 0;
        for ( int x = 0 ; x < userInput.size(); x++)
        {
            sum += userInput.get(x) ;
        }
        avg = sum / userInput.size();   
}

public static double getAvg ()
{
    return avg;
}

}

Main:

import javax.swing.*;
import java.util.*;

public class try1
{   

public static ArrayList<Integer>user=new ArrayList<Integer>();
private static try2 testing = new try2 (user);
public static Integer testnum;  

public static void main (String[] args)
{
    testnum = Integer.parseInt(JOptionPane.showInputDialog(null, "Please Enter The Amount Of Test To Be Calculated Below "));
classes ();
}


public static void classes ()
{
    int userInput = 0;
            if (userInput == JOptionPane.YES_OPTION)
            {
                for ( int count = 1; count <= testnum; count++)
                {   
                    String userInputString = JOptionPane.showInputDialog(null, " PLEASE ENTER ALL THE FOLLOWING TEST GRADES TO CALCULATE ");
                    int value = Integer.parseInt(userInputString);
                    user.add(value);
                }

            if (userInput == JOptionPane.NO_OPTION )
            {
                testing.setAvg ();
                JOptionPane.showMessageDialog(null,"You average is" + (testing.getAvg()));
            }
    }
}
Erick
  • 19
  • 1
  • 7
  • 1
    Because that's `InputMismatchException`... – Luiggi Mendoza Feb 19 '14 at 02:40
  • @ luiggi , Ok I tried it still doesn't pic anything up, my guess is that my for statement is not working properly – Erick Feb 19 '14 at 02:42
  • Seriously, your code needs something more than a `try-catch` statement to work... – Luiggi Mendoza Feb 19 '14 at 02:43
  • All of these answers are wrong. Your `test` is an arraylist, so you must check to see if the *current element* is outside the range. Instead of checking if `test < 0`, use `test.get(i) < 0`. Also, use `test.size()`, not `test.size(x)`. – kevinsa5 Feb 19 '14 at 02:50
  • @ kevinsa5 , you a right .get makes more sense, however it is still not catching a negative input or anything over 100. Would you advise to use another approach to catching a negative input. – Erick Feb 19 '14 at 02:59
  • Your `catch` doesn't line up with your `try` block, it has to go right after it. Right now, it's after your `for` loop. Also, you're looping from zero to `test.size()`, which is actually one more element than what the list contains-- remember that java indexes from zero. You should loop until `test.size()-1`. – kevinsa5 Feb 19 '14 at 03:07
  • @ Keninsa ok I implemented what you suggested, but it is not finding the negative input. my for statement is running well do you think it is the conditional statement that is not getting my values from my main? – Erick Feb 19 '14 at 03:26

2 Answers2

0

Ok. Here is working code. I just modified your code. According to Naming standard, you should use Class name like 'Try1' 'Try2'. And you should immediately throw exception if you don't wanna accept negative value. But according to your code, here it is.

In try2 class,

public try2(ArrayList<Integer> test) {
        for (int x = 0; x <= test.size()-1; x++) {
            try {
                if (test.get(x) < 0 || test.get(x) > 100) {
                    throw new IllegalArgumentException();
                } else {
                    this.userInput = test;
                }
            } catch (IllegalArgumentException ex) {
                JOptionPane.showMessageDialog(null, " NO NEGETIVES ALLOWED ");
            }
        }
    }

In try1 class,

public static void main(String[] args) {
        testnum =
                Integer.parseInt(JOptionPane.showInputDialog(null,
                        "Please Enter The Amount Of Test To Be Calculated Below "));
        classes();
    }

    public static void classes() {
        int userInput = 0;
        if (userInput == JOptionPane.YES_OPTION) {
            user = new ArrayList<Integer>();
            for (int count = 1; count <= testnum; count++) {
                String userInputString =
                        JOptionPane.showInputDialog(null, " PLEASE ENTER ALL THE FOLLOWING TEST GRADES TO CALCULATE ");
                int value = Integer.parseInt(userInputString);
                user.add(value);
            }

            if (userInput == JOptionPane.NO_OPTION) {
                testing.setAvg();
                JOptionPane.showMessageDialog(null, "You average is" + (testing.getAvg()));
            }

            new try2(user);

        }
    }

I only updated the codes I modified. You need to modify as you need.

Min Naing Oo
  • 1,085
  • 5
  • 24
  • 49
  • Are you sure the poster meant `test.size() < 0`? That's not possible, for one, and it doesn't make sense to have that in a loop. I think they meant `test.get(i) < 0 || test.get(i) > 100`. – kevinsa5 Feb 19 '14 at 02:52
  • Yeah. My mistake. sorry – Min Naing Oo Feb 19 '14 at 03:00
  • Ok Take a look at my whole code, for some reason it is not catching a negative, and it is throwing an arithmetic error. It says it is dividing by zero? I am summing up the array list list with "size", how is it dividing by 0? – Erick Feb 19 '14 at 04:31
  • @ Min Naing Oo Hey thank you for your help I got my program done, I took your advice and it really made a world of difference , I will post my entire code later on today so you can see it :) – Erick Feb 19 '14 at 08:18
  • @Erick You shouldn't update corrected code in your question. Don't forget to accept the question by clicking Tick button if my advice solved your problem. – Min Naing Oo Feb 19 '14 at 08:24
-2

use this code it will solve your problem

 public try2(ArrayList<Integer> test) 
   {
     for ( int x = 0 ; x <= test.size(x); x++)
       {
        try
            {
                if ( test < 0 || test > 100)
                {
                    throw new IllegalArgumentException ();
                }

                else
                {
                    this.userInput(x) = test(x);    
                }
            }
            catch ( IllegalArgumentException ex)
            {
                    JOptionPane.showMessageDialog(null," NO NEGETIVES ALLOWED ");
            }// end of try

          }// end of for loop
  }// end of program
user1195450
  • 141
  • 1
  • 1
  • 7
  • There are several problems in this code: `test.size(x)` doesn't compile, `test < 0` doesn't compile, `test > 100` doesn't compile. `this.userInput(x) = test(x)` won't compile... – Luiggi Mendoza Feb 19 '14 at 02:47