Results 1 to 6 of 6
Thread: exception error
- 10-12-2009, 12:24 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 14
- Rep Power
- 0
exception error
I'm trying to throw IndexOutOfBoundsException when index is bigger than array or smaller than 0.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 }
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
-
Other than missing a closing parenthesis, it looks good to me. Could there be a problem in the tester method?
- 10-12-2009, 01:06 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 14
- Rep Power
- 0
I figured out the problem, I was suppose to use Size() instead of values.length.
but now I have another problem..
It is suppose to print out array like [3, 4, 5, ,6 ] etcetc..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(); }
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..?
-
This line is wrong:
should it beJava Code:if(i<values.length-1) {
since you want to add a comma delimiter if you are not at the last value.Java Code:if(i != values.length-1) { // if not the last valueLast edited by Fubarable; 10-12-2009 at 01:33 AM.
- 10-12-2009, 02:35 AM #5
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
- 10-12-2009, 07:46 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 14
- Rep Power
- 0
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
-
Exception error
By Rose88 in forum New To JavaReplies: 8Last Post: 07-06-2009, 10:22 PM -
Exception error
By jaiminparikh in forum New To JavaReplies: 0Last Post: 03-20-2009, 09:06 PM -
Exception Error need help fixing
By skinnybones in forum New To JavaReplies: 2Last Post: 12-03-2007, 07:14 PM -
JSF error+exception
By Peter in forum SWT / JFaceReplies: 1Last Post: 07-04-2007, 06:29 AM -
tomcat exception-error
By Nick15 in forum EclipseReplies: 2Last Post: 05-11-2007, 01:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks