I see your problem.
You are trying to
dynamically change the size of your arrays by reintializing them at the top of your loop, like "String names[] = new String[size];"
By doing so, you are clearing out your array, so it's in effect a new array. That is why you are getting
null values. Nothing is in them any more after you redeclare the string size.
Arrays in Java are static data structures. You've got to know how big they are going to be when you declare them. You are trying to use them dynamically, making them grow as your programs runs, which won't work.
To make them dynamic, grow at runtime, you could use the Vector class, but it's better to use the newer
ArrayList class, imo.
Code:
ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> grades = new ArrayList<Integer>();
Note, because the ArrayList class takes objects, not primitives, you'll have to use the
Integer class instead of the "int" primitive datatype, which are not objects.
Also be sure not to put your ArrayList declaration inside your loop, but declare it outside the loop so it won't get redeclared during every iteration of your loop and wiped out. Important.
With ArrayList, you can
dynamically add new indexes via the
add() method. In your case, you are adding the value from your console to the names ArrayList, so you would do:
Code:
names.add(reader.next());
To read the values, use the
get() method. If inside a loop and you want to read the index i of names, do this:
To get the length of an ArrayList, you use the
size() method.
As with Arrays, you can still use equals() method, so this will still work in your if conditional statement:
Code:
if (names.get(i).equals("end")) {
That should give you enough info to redo your code using ArrayList.
You can read more about ArrayList
here.
Be sure to post back to let us know how you are progressing.