- #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:
However after inputting the input n I received this error: ArrayIndexOutofBounds Exception
I need to find out whats the problem
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);
}
}
I need to find out whats the problem
Last edited by a moderator: