Indexing Problem with Java Program: ArrayIndexOutOfBounds Exception

  • Context: Java 
  • Thread starter Thread starter Trollfaz
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The forum discussion addresses an ArrayIndexOutOfBounds Exception encountered in a Java program that processes input lines formatted as "name value". The issue arises from the incorrect assumption that the split operation will always return three parts, leading to attempts to access an index that does not exist. The solution involves checking the length of the array returned by the split method and ensuring that the input format matches expectations. Additionally, the use of two hash tables is deemed unnecessary, as only one is utilized in the final output.

PREREQUISITES
  • Understanding of Java programming and syntax
  • Familiarity with the Hashtable class in Java
  • Knowledge of exception handling in Java
  • Experience with string manipulation and array handling in Java
NEXT STEPS
  • Learn about Java exception handling techniques
  • Research the differences between Hashtable and HashMap in Java
  • Explore the use of ArrayList for dynamic array management in Java
  • Study the String.split() method and its behavior with different input formats
USEFUL FOR

Java developers, software engineers, and students learning about input processing and error handling in Java applications.

Trollfaz
Messages
144
Reaction score
16
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   Reactions: postorous
Technology news on Phys.org
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.
 
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.
 
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.
 
Hard to read without code tags.
 
  • Like
Likes   Reactions: Filip Larsen
Shouldn't this line:
Java:
int n=sc.nextInt()+1;
Be:
Java:
int n=sc.nextInt();
 
Borg said:
Hard to read without code tags.
Now fixed.
 
  • Like
Likes   Reactions: Wrichik Basu, Filip Larsen and Borg
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   Reactions: harborsparrow
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.
 

Similar threads

Replies
2
Views
2K
  • · Replies 28 ·
Replies
28
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
8K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K