Results 1 to 6 of 6

Thread: exception error

  1. #1
    kira137 is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default exception error

    Java Code:
    /**
      * Get the value at the given index in this list.
      * @throws IndexOutOfBoundsException if index is not in [0..size-1]
      */
    
    public int get(int index) throws IndexOutOfBoundsException{
       if(index>=values.length){
         throw new IndexOutOfBoundsException ("index is bigger than array");
       } else if(index<0){
         throw new IndexOutOfBoundsException ("index is smaller than 0");
       } else {
         return values[index]; 
        // this should only return values explicitly added or set
       }
    I'm trying to throw IndexOutOfBoundsException when index is bigger than array or smaller than 0.
    So I ran this through the tester that prof gave us, but it reads as 'no exception' and fails the program..
    What did I do wrong in this code?

    thank you in advance

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

    Default

    Other than missing a closing parenthesis, it looks good to me. Could there be a problem in the tester method?

  3. #3
    kira137 is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    I figured out the problem, I was suppose to use Size() instead of values.length.
    but now I have another problem..

    Java Code:
    StringBuilder a = new StringBuilder("[ ");
       for(int i = 0; i <values.length; i++){
         if(i<values.length-1) {
           a.append(values[i] + ", ");
         } else {
           a.append(values[i]);
         }
       }
       a.append(" ]");
      return a.toString();
     }
    It is suppose to print out array like [3, 4, 5, ,6 ] etcetc..
    I'm very new to using StringBuilder.. so I tried to copy from one of the notes I took in class. But again, it said 'fail' in the tester program.
    Did I make a mistake somewhere..?

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

    Default

    This line is wrong:
    Java Code:
    if(i<values.length-1) {
    should it be
    Java Code:
    if(i != values.length-1) {   // if not the last value
    since you want to add a comma delimiter if you are not at the last value.
    Last edited by Fubarable; 10-12-2009 at 01:33 AM.

  5. #5
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by kira137 View Post
    I figured out the problem, I was suppose to use Size() instead of values.length.
    but now I have another problem..

    Java Code:
    StringBuilder a = new StringBuilder("[ ");
       for(int i = 0; i <values.length; i++){
         if(i<values.length-1) {
           a.append(values[i] + ", ");
         } else {
           a.append(values[i]);
         }
       }
       a.append(" ]");
      return a.toString();
     }
    It is suppose to print out array like [3, 4, 5, ,6 ] etcetc..
    I'm very new to using StringBuilder.. so I tried to copy from one of the notes I took in class. But again, it said 'fail' in the tester program.
    Did I make a mistake somewhere..?
    What type is the variable, values? I tested it with an int[], and it works fine(after removing the extra '}' at the end, not sure if that's the error).

    Java Code:
    import java.util.Arrays;
    
    /**
     * A test
     */
    public class Test
    {
    	/**
    	 * @param args
    	 *            not used
    	 */
    	public static void main(String[] args)
    	{
    		int[] values = new int[] { 3, 4, 5, 6 };
    
    		// did you mean to have a space after the '['
    		StringBuilder a = new StringBuilder("[ ");
    		for (int i = 0; i < values.length; i++) {
    			if (i < values.length - 1) {
    				a.append(values[i] + ", ");
    			} else {
    				a.append(values[i]);
    			}
    		}
    		// did you mean to have a space before the ']'
    		a.append(" ]");
    
    		System.out.println(a.toString());
    
    		// why not use Arrays.toString method (requires Java 1.5+)
    		System.out.println(Arrays.toString(values));
    	}
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  6. #6
    kira137 is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    thank you for the replies.. but the tester still fails my code. The values array is int-
    Perhaps I should use string..?
    Ah, I forgot the comment for toString() requirement
    toString() is suppose to do this:

    /**
    * Convert this list to a String.
    * For example: "[ 3, 15, 0, 22 ]"
    */
    public String toString() {
    (mycode from above)
    }

Similar Threads

  1. Exception error
    By Rose88 in forum New To Java
    Replies: 8
    Last Post: 07-06-2009, 10:22 PM
  2. Exception error
    By jaiminparikh in forum New To Java
    Replies: 0
    Last Post: 03-20-2009, 09:06 PM
  3. Exception Error need help fixing
    By skinnybones in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 07:14 PM
  4. JSF error+exception
    By Peter in forum SWT / JFace
    Replies: 1
    Last Post: 07-04-2007, 06:29 AM
  5. tomcat exception-error
    By Nick15 in forum Eclipse
    Replies: 2
    Last Post: 05-11-2007, 01:32 AM

Posting Permissions

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