Hi all, first post here, I'm having some trouble on a critical part of a program I'm writing for a programming class I'm currently taking. The entire program is pretty large (10+ classes, some well over 100 lines) so I won't post the whole thing here, but here's the part where I'm stuck...
I have this class IPaddress, which is fairly simple. Basically, the constructor takes in a string in format "xxx.xxx.xxx.xxx" (for example, "192.168.1.255"). Then it splits it up into four shorts in an array using StringTokenizer, and stores them and is able to output each "field" separately (that's what those get methods are).
import java.util.StringTokenizer; public class IPaddress { private static short[] address = new short[4]; private static final byte FIELD_A = 0, FIELD_B = 1, FIELD_C = 2, FIELD_D = 3; public IPaddress(String input) { StringTokenizer theTokenizer = new StringTokenizer(input, "."); for(byte i = 0; i < 4; i++) address[i] = Short.parseShort(theTokenizer.nextToken()); } public short getFieldA() { return(address[FIELD_A]); } public short getFieldB() { return(address[FIELD_B]); } public short getFieldC() { return(address[FIELD_C]); } public short getFieldD() { return(address[FIELD_D]); } }
In another class, I have these two static methods, ReadLogData and ProcessLine (ReadLogData I invoke and ProcessLine is invoked by the first method). RedLogData is passed in the string of a file path, and then chops up the file by line and processes each line to get the string format I mentioned above. Then it returns an array of IPaddress objects.
public class WebLogData { private static int numberOfHits; private static final int DEFAULT_SIZE = 20000; public static String ProcessLine(String theLine) //take in single line { StringTokenizer theTokenizer = new StringTokenizer(theLine, " "); //string tokenizer with delimiter return(theTokenizer.nextToken()); //takes first part of line and discards the rest } public static IPaddress[] ReadLogData(String theFileName) //take in file name { IPaddress[] addressList = new IPaddress[DEFAULT_SIZE]; //array of IPaddress objects try { File theFile = new File(theFileName); //file reader Scanner theScanner = new Scanner(theFile); //scanner for(numberOfHits = 0; theScanner.hasNext(); numberOfHits++) //run while there are more lines addressList[numberOfHits] = new IPaddress(ProcessLine(theScanner.nextLine())); //element object IPaddress gets its constructor invoked theScanner.close(); //close scanner } catch(FileNotFoundException e) //catch exception { e.printStackTrace(); } return(addressList); } public static int GetNumberOfHits() { return(numberOfHits); } }
Now the problem: when I invoke the constructor addressList[numberOfHits] = new IPaddress(ProcessLine(theScsanner.nextLine())), it doesn't just construct an object IPaddress at addressList[numberOfHits], it constructs an object there and then modifies all previous objects in the array that have been constructed! So not just the object at addressList[numberOfHits], but the object at addressList[numberOfHits - 1], addressList[numberOfHits - 2], etc. This is screwing up my whole program! I can't continue debugging until I get this fixed
Thanks in advance for any help.