Java Indexing Problem with Java Program: ArrayIndexOutOfBounds Exception

  • Thread starter Thread starter Trollfaz
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion centers around a Java program designed to read multiple lines of input formatted as 'name value' and store them in hash tables. The primary issue encountered is an ArrayIndexOutOfBoundsException, which arises from attempting to access an index in an array that does not exist. This error typically occurs when the input line does not contain the expected number of parts after being split. Key points include the suggestion to modify the input handling by correcting the way the number of expected lines is defined, changing the line from `int n=sc.nextInt()+1;` to `int n=sc.nextInt();`. Additionally, there is a critique of using two hash tables, as the program only returns one, making the second redundant. It is also recommended to implement exception handling to manage invalid input more effectively, emphasizing the importance of validating input when processing external data. The discussion highlights the need for clarity in input format and the potential pitfalls of array handling in Java.
Trollfaz
Messages
143
Reaction score
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:
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 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 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 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
1
Views
1K
Replies
9
Views
3K
Replies
2
Views
2K
Replies
3
Views
3K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
11
Views
5K
Back
Top