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
137
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.
 

1. What is an ArrayIndexOutOfBoundsException?

An ArrayIndexOutOfBoundsException is a type of error that occurs when trying to access an element in an array using an index that is either negative or greater than the size of the array.

2. How does an ArrayIndexOutOfBoundsException occur?

This error occurs when a program tries to access an index that is not within the bounds of the array. This can happen if the index is entered incorrectly or if the array is not large enough to hold the specified number of elements.

3. How can I fix an ArrayIndexOutOfBoundsException in my Java program?

To fix this error, you will need to review your code and make sure that the index being used to access the array is within the bounds of the array. You can also use try-catch blocks to handle the error and prevent it from crashing your program.

4. Can an ArrayIndexOutOfBoundsException be prevented?

Yes, this error can be prevented by carefully reviewing your code and ensuring that the index being used to access the array is within the bounds of the array. You can also use conditional statements to check the length of the array before accessing it.

5. Is an ArrayIndexOutOfBoundsException a common error in Java programming?

Yes, this error is fairly common in Java programming, especially for beginners. It can be caused by simple mistakes such as using incorrect indexes or not properly checking the length of the array before accessing it. However, with proper coding practices and error handling, this error can be easily avoided.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
Back
Top