Possible Duplicate:
Printing a list in java
I have different variables in a file that I need to assign to different variable arrays. Here is what is in the file:
Lee Keith Austin Kacie Jason Sherri Jordan Corey Reginald Brian Taray
Christopher Randy Henry Jeremy Robert Joshua Robert Eileen
Cassandra Albert Russell Ethan Cameron Tyler Alex Kentrell rederic
10 20 100 80 25 35 15 10 45 55 200 300 110 120 111 7 27 97 17 37
21 91 81 71 16 23 33 45
A b c w e r t q I u y b G J K S A p o m b v x K F s q w
11.5 29.9 100 200 115.1 33.3 44.4 99.9 100.75 12.2 13.1 20.3 55.5 77.7
12.1 7.1 8.2 9.9 100.1 22.2 66.6 9.9 1.25 3.75 19.9 3.321 45.54 88.8
And here is my code:
import java.util.*;
public class Prog5Methods{
public Prog5Methods(){
}
public void ReadFromFile(Scanner input, String [] names, int [] numbers, char [] letters, double [] num2){
System.out.println("\nReading from a file...\n");
System.out.println("\nDONE\n");
int r = 0;
while(input.hasNext()){
names[r] = input.next();
numbers[r] = input.nextInt();
letters[r] = input.next().charAt(0);
num2[r] = input.nextDouble();
r++;
}
} // end of readFromFile
I am getting an IndexOutOfBounds exception every time. How can I assign these types to these arrays?
You need to make sure that the arrays passed into the function have the right length set. If you are getting
IndexOutOfBoundsexception, you are probably not initializing the arrays based on whats in the file.Another option is to use a
List, which has no size restriction, and then to convert the lists into arrays. See this for an easy way to do that.