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)
Re: Out of bounds Exception in Array
Something's wrong with the EasyIn code?
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());
}
}
}
}
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?
Re: Out of bounds Exception in Array
Sorry about that. I don't know what a Scanner is, will look into it.
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.
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.