Results 1 to 7 of 7
  1. #1
    Catto is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Out of bounds Exception in Array

    I am trying to write a program that allows the user to enter the number of competitors in a race (which will then make the array size) and then enter the time it took each competitor to run the race into the array. I'm getting an out of bounds exception before I can even set what the bounds would be at runtime. I'm new to JAVA and struggling with this, so there could be any number of errors is this code, all input would be appreciated!

    class compareTimes
    {
    public static void main(String[] args)
    {
    //set array size at runtime
    int arraySize = EasyIn.getInt();

    //declare and create array raceTimes
    double raceTimes[] = new double[arraySize];

    //call method for code to enter raceTimes
    enterTimes (raceTimes);

    //code to display times
    System.out.println();
    System.out.println("***RACETIMES ENTERED***");
    for (int i = 0; i < arraySize; i++)
    {
    System.out.println("racetime" + (i+1) + " " + raceTimes[i]);
    }

    EasyIn.pause("press <Enter> to Quit");
    }

    //code to enter raceTimes
    private static void enterTimes(double timesIn[])
    {
    for (int i = 0; i < timesIn.length; i++)
    {
    System.out.println("time for race" + (i+1) );
    timesIn[i] = EasyIn.getDouble();
    }
    }
    }






    And the error message:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
    at java.lang.String.checkBounds(String.java:397)
    at java.lang.String.<init>(String.java:569)
    at EasyIn.getInt(EasyIn.java:40)
    at compareTimes.main(compareTimes.java:6)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: Out of bounds Exception in Array

    Something's wrong with the EasyIn code?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Catto is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Out of bounds Exception in Array

    Just tried using the EasyIn with more basic code there and it didn't work. Looks like that's where the problem lies. I didn't write that code myself and don't understand half of it. Any ideas?

    // EasyIn.java

    import java.io.*;

    public abstract class EasyIn
    {
    static String s = new String();
    static byte[] b = new byte[512];
    static int bytesRead = 0;

    public static String getString()
    {
    boolean ok = false;
    while(!ok)
    {
    try
    {
    bytesRead = System.in.read(b);
    s = new String(b,0,bytesRead-1);
    s=s.trim();
    ok = true;
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    return s;
    }

    public static int getInt()
    {
    int i = 0;
    boolean ok = false;
    while(!ok)
    {
    try
    {
    bytesRead = System.in.read(b);
    s = new String(b,0,bytesRead-1);
    i = Integer.parseInt(s.trim());
    ok = true;
    }
    catch(NumberFormatException e)
    {
    System.out.println("Make sure you enter an integer");
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    return i;
    }

    public static byte getByte()
    {
    byte i = 0;
    boolean ok = false;
    while(!ok)
    {
    try
    {
    bytesRead = System.in.read(b);
    s = new String(b,0,bytesRead-1);
    i = Byte.parseByte(s.trim());
    ok = true;
    }
    catch(NumberFormatException e)
    {
    System.out.println("Make sure you enter a byte");
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    return i;
    }

    public static short getShort()
    {
    short i = 0;
    boolean ok = false;
    while(!ok)
    {
    try
    {
    bytesRead = System.in.read(b);
    s = new String(b,0,bytesRead-1);
    i = Short.parseShort(s.trim());
    ok = true;
    }
    catch(NumberFormatException e)
    {
    System.out.println("Make sure you enter a short integer");
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    return i;
    }


    public static long getLong()
    {
    long l = 0;
    boolean ok = false;
    while(!ok)
    {
    try
    {
    bytesRead = System.in.read(b);
    s = new String(b,0,bytesRead-1);
    l = Long.parseLong(s.trim());
    ok = true;
    }
    catch(NumberFormatException e)
    {
    System.out.println("Make surre you enter a long integer");
    }

    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    return l;
    }


    public static double getDouble()
    {
    double d = 0;
    boolean ok = false;
    while(!ok)
    {
    try
    {
    bytesRead = System.in.read(b);
    s = new String(b,0,bytesRead-1);
    d = (Double.valueOf(s.trim())).doubleValue();
    ok = true;
    }
    catch(NumberFormatException e)
    {
    System.out.println("Make sure you enter a decimal number");
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    return d;
    }

    public static float getFloat()
    {
    float f = 0;
    boolean ok = false;
    while(!ok)
    {
    try
    {
    bytesRead = System.in.read(b);
    s = new String(b,0,bytesRead-1);
    f = (Float.valueOf(s.trim())).floatValue();
    ok = true;
    }
    catch(NumberFormatException e)
    {
    System.out.println("Make sure you enter a decimal number");
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    return f;
    }

    public static char getChar()
    {
    char c = ' ';
    boolean ok = false;
    while(!ok)
    {
    try
    {
    bytesRead = System.in.read(b);
    s = new String(b,0,bytesRead-1);
    if(s.trim().length()!=1)
    {
    System.out.println("Make sure you enter a single character");
    }
    else
    {
    c = s.trim().charAt(0);
    ok = true;
    }
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    return c;
    }


    public static void pause()
    {
    boolean ok = false;
    while(!ok)
    {
    try
    {
    System.in.read(b);
    ok = true;
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    }

    public static void pause(String messageIn)
    {
    boolean ok = false;
    while(!ok)
    {
    try
    {
    System.out.print(messageIn);
    System.in.read(b);
    ok = true;
    }
    catch(IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: Out of bounds Exception in Array

    Can you please use [code] tags [/code] when posting code.
    I can't read that lot.

    What I can read begs the question, why are you not using a Scanner?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Catto is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Out of bounds Exception in Array

    Sorry about that. I don't know what a Scanner is, will look into it.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: Out of bounds Exception in Array

    Where did you get the EasyIn from, out of curiosity?
    It's the second time I've seen it mentioned this past week or so.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Out of bounds Exception in Array

    Check the bounds, the program is trying to access data which isnt there. For example, trying to get to array[5], when the size is only 4. The same could be said when accesing a string index.

Similar Threads

  1. Help Array Index out of bounds exception
    By star400040 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 10:24 PM
  2. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-18-2010, 11:25 PM
  3. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 04:02 PM
  4. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM
  5. Array out of bounds exception 20.
    By dropt in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 10:32 PM

Tags for this Thread

Posting Permissions

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