I am trying to use a scanner object to initialize an array object from a class but am stuck on how to do it.
here is my main method
input
static class MyDataStructure implements Comparable<MyDataStructure> { //___________________Variable Declaration_______________________________ private int testCases; private int numProblems; private int[] timeForCompletion; private int[] weight; //___________________Constructor_________________________________________ public MyDataStructure() { testCases = 0; numProblems = 0; timeForCompletion = new int[numProblems]; weight = new int[numProblems]; } //_________________Accessors/Mutators____________________________________ /** * @return the testCases */ public int getTestCases () { return testCases; } /** * @param testCases * the testCases to set */ public void setTestCases (int testCases) { this.testCases = testCases; } /** * @return the numProblems */ public int getNumProblems () { return numProblems; } /** * @param numProblems * the numProblems to set */ public void setNumProblems (int numProblems) { this.numProblems = numProblems; } /** * @return the timeForCompletion */ public int[] getTimeForCompletion () { return timeForCompletion; } /** * @param timeForCompletion * the timeForCompletion to set */ public void setTimeForCompletion (int[] timeForCompletion) { this.timeForCompletion = timeForCompletion; } /** * @return the weight */ public int[] getWeight () { return weight; } /** * @param weight * the weight to set */ public void setWeight (int[] weight) { this.weight = weight; }
here is my main method
public static void main (String[] args) { Scanner input = new Scanner (System.in); MyDataStructure ds = new MyDataStructure (); while (input.hasNext ()) { // initialize all values of ds ds.setTestCases (input.nextInt ()); ds.setNumProblems (input.nextInt ()); //problem is here!!! for(int i = 0; i< ds.numProblems; i++){ ds.setTimeForCompletion(input.nextInt ()); } }
input
1 2 2 3 12 4