Java Homework Array / getter / setter / object help via /r/learnprogramming


Java Homework Array / getter / setter / object help

I have a file with 4000+ lines in it. Each line contains a name and 11 numbers that represent the names rank for a decade.

John 1 2 3 4 5 6 7 8 9 10 11

Cathy 1 2 3 4 5 6 7 8 9 10 11

etc..

I read the file in, put each line into an ArrayList and then converted the ArrayList to a normal array. Then I took the array of lines and I'm trying to split it into a new array with 12 elements.

object.Array[0] = Name

object.Array[1] = Decade 1

object.Array[2] = Decade 2

etc..

I'm getting an ArrayIndexOutOfBoundsException and something that says 5 internal calls.. Not sure what I did wrong but I think it's in my setters / getters and trying to set the name and decades..

Here is my code:

public class Name { String childName = "N/A"; // Name rank for decade.. 11 decades [0] - [10] int[] decadeRank = new int[11]; public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("names.txt")); // read lines from file and store in array list ArrayList<String> nameList = new ArrayList<String>(); while (input.hasNextLine()) { nameList.add(input.nextLine()); } // convert array list into array String[] nameArr = new String[nameList.size()]; nameArr = nameList.toArray(nameArr); // split line into name and decades String[] lineSplit = nameArr[0].split(" "); //Create ArrayList of Objects from File // assign values to class object Name test1 = new Name(); for (int i = 0; i < lineSplit.length; i++) { test1.setChildName(lineSplit[i]); for(int k = 1; k < lineSplit.length; k++) { int x = Integer.parseInt(lineSplit[k]); test1.setDecadeRank(k, x); } } System.out.println(nameArr[0]); System.out.println("Name: " + test1.childName); System.out.println("Decade 1: " + test1.decadeRank[0]); System.out.println("Decade 2: " + test1.decadeRank[2]); System.out.println("Decade 3: " + test1.decadeRank[3]); System.out.println("Decade 4: " + test1.decadeRank[4]); System.out.println("Decade 5: " + test1.decadeRank[5]); System.out.println("Decade 6: " + test1.decadeRank[6]); System.out.println("Decade 7: " + test1.decadeRank[7]); System.out.println("Decade 8: " + test1.decadeRank[8]); System.out.println("Decade 9: " + test1.decadeRank[9]); System.out.println("Decade 10: " + test1.decadeRank[10]); System.out.println("Decade 11: " + test1.decadeRank[11]); } public String getChildName() { return childName; } public void setChildName(String childName) { this.childName = childName; } public int[] getDecadeRank() { return decadeRank; } public void setDecadeRank(int decadeRank, int rank) { this.decadeRank[decadeRank] = rank; } } 

Submitted July 17, 2017 at 04:46PM by b0red88
via reddit http://ift.tt/2u2DsoI

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s