Indexing Problem with Java Program: ArrayIndexOutOfBounds Exception

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

Discussion Overview

The discussion revolves around a Java programming issue related to an ArrayIndexOutOfBounds Exception encountered when processing input for a hashtable. Participants explore the code structure, input handling, and potential errors in the implementation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the issue with the program and the specific error encountered when trying to read input into a hashtable.
  • Another participant suggests that Java arrays are of fixed size and recommends using a list for dynamic sizing.
  • A participant emphasizes the need to check the length of the array returned by the split method, indicating that the exception may arise from insufficient parts being returned.
  • There is a suggestion to revise the line that increments the input count, proposing that it should simply read the input without adding one.
  • One participant notes the redundancy of using two hashtables, questioning the necessity based on the input format described.
  • Another participant advises on the importance of catching exceptions when processing external input to handle potential errors effectively.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of two hashtables and the handling of input. There is no consensus on the best approach to resolve the error, and multiple perspectives on input handling and error management are presented.

Contextual Notes

Participants highlight potential issues with input format assumptions and the handling of array lengths, indicating that the discussion is dependent on the specific structure of the input data.

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
4K
  • · 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