Indexing Problem with Java Program: ArrayIndexOutOfBounds Exception

  • Java
  • Thread starter Trollfaz
  • Start date
  • Tags
    Java
In summary, the problem appears to be that the Java compiler does not like input that is not in the expected format.
  • #1
Trollfaz
141
14
I encounter this issue when trying to make this program in Java.
My first input is a number n to indicate how many lines of input for Java to expect next. For each of my next n lines of input, it is in this format
'name value', e.g 'bolt 9.58'. Then for each line, I want java to append the name, value into a hashtable in the format (String name, float value).
The following is my code:
Java:
import java.util.*;
import java.io.*;
public class bestrelay{
    public static void main(String[]args){
        Scanner sc= new Scanner(System.in);
        int n=sc.nextInt()+1;
        Hashtable<String,Float> data1=new Hashtable<String,Float>();
        Hashtable<String,Float> data2=new Hashtable<String,Float>();
        for (int i=0;i<n;i++){
            String next_=sc.nextLine();
            String[] parts= next_.split(" ");
            data1.put(parts[0],Float.parseFloat(parts[1]));
            data2.put(parts[0],Float.parseFloat(parts[2]));

        }
    System.out.println(data1);
    }
}
However after inputting the input n I received this error: ArrayIndexOutofBounds Exception
I need to find out whats the problem
 
Last edited by a moderator:
  • Like
Likes postorous
Technology news on Phys.org
  • #2
Java arrays have their size predefined and can't be resized on the fly. If you want to increase the size as you add items, you should use a list.
 
  • #3
You need to check the length of the array returned from split. I assume the exception you are getting is from this line which means the split did give less than 3 parts.
 
  • #4
Borg said:
Java arrays have their size predefined and can't be resized on the fly.
In this case the array is returned by split.
 
  • #5
Hard to read without code tags.
 
  • Like
Likes Filip Larsen
  • #6
Shouldn't this line:
Java:
int n=sc.nextInt()+1;
Be:
Java:
int n=sc.nextInt();
 
  • #7
Borg said:
Hard to read without code tags.
Now fixed.
 
  • Like
Likes Wrichik Basu, Filip Larsen and Borg
  • #8
Some of the functions called will throw an exception. To write good code, especially if it is misbehaving, you should get in the habit of catching exceptions and displaying them when they happen.
This is especially true when a lot of external input is being processed. You might be surprised at how frequently the input is invalid.
 
  • Like
Likes harborsparrow
  • #9
Why does your program use 2 hash tables? That's not in the specification.
Your programs appears to read lines of the from "bolts 1.4 1.2" and then put ("bolts", 1.4) in the first hash-table and ("bolts", 1.2) in the second hashtable. At the end, only the first hash table is returned. making all operations with the second hash table redundant.
If your input lines are of the form "<string> <floating point number>" this will result in an error when trying to use parts[2].
If your input line are really of the form "<string> <floating point number> <floating point number>", I think you'll get the problem mentioned by jack action, and your program will likely go wrong when reading the last non-existing line.
 

FAQ: Indexing Problem with Java Program: ArrayIndexOutOfBounds Exception

Why am I getting an ArrayIndexOutOfBounds Exception in my Java program?

An ArrayIndexOutOfBounds Exception occurs when you try to access an index in an array that is outside the bounds of the array. This can happen if you are trying to access an index that is less than 0 or greater than or equal to the length of the array.

How can I fix an ArrayIndexOutOfBounds Exception in my Java program?

To fix an ArrayIndexOutOfBounds Exception, you should check the index you are trying to access and make sure it is within the bounds of the array. You can also use try-catch blocks to handle the exception and prevent your program from crashing.

What are some common causes of ArrayIndexOutOfBounds Exceptions in Java programs?

Some common causes of ArrayIndexOutOfBounds Exceptions include accessing an index that is out of bounds, not initializing the array properly, or not checking the length of the array before accessing an index.

How can I prevent ArrayIndexOutOfBounds Exceptions in my Java program?

To prevent ArrayIndexOutOfBounds Exceptions, you should always check the length of the array before accessing an index, use try-catch blocks to handle exceptions, and make sure to properly initialize your arrays before using them.

Are there any tools or techniques that can help me debug ArrayIndexOutOfBounds Exceptions in Java programs?

Yes, there are several tools and techniques that can help you debug ArrayIndexOutOfBounds Exceptions in Java programs. You can use a debugger to step through your code and see where the exception is occurring, use logging to track the values of variables, and use unit tests to catch potential issues before they occur in production.

Similar threads

Replies
1
Views
1K
Replies
9
Views
3K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
4
Views
2K
Replies
1
Views
1K
Replies
11
Views
5K
Back
Top