Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

java - Reading a text file of unknown length into an array of objects without arraylist

I know that the title sounds redundant but its for a exercise, otherwise I would use an arraylist. I am sorry if this been asked before, I tried to find a solution but couldn't seem to. My program is to read a text file of an unknown length into an array object (line 1,2,3,4 equals one object). I was tasked to make the object array 1000 and then program a way to see how many of the objects are then created in the array, to use THAT number as my array for other classes, like print or searching the array.

So this is my class that scans the file and makes the array of objects

UPDATE: I FIXED THE ISSUE, BELOW IS THE ORGINAL CODE SUBMITTED. THE FINALIZED FIXED CODE IS AT THE BOTTOM. GOOD LUCK FELLOW STUDENTS :) read the comments as well. Only thing I have to do left on my side is change the inEmploy/inSalary to ints and the orginal setters to int and then parse

public void loadArray() throws NoSuchElementException, IOException, ArrayIndexOutOfBoundsException {
    String inCOS = "";
    String inTITLE = "";
    int inEMPLOY = 0;
    int inSALARY = 0;
    java.io.File file = new java.io.File("Lions.txt");
    Scanner infile = new Scanner(file);
    //load array up to 1000 and use the while loop to find the real number of slots
    //use that variable for other pieces
    while (infile.hasNextLine()) {
        MakeList[i] = new Occupations(inCOS, inTITLE, inEMPLOY, inSALARY);
        i++;
    }
}

}

Running the program gives a index out of bounds exception obviously. I just don't understand how you could make an array that would take null values on purpose(I assume) or you would make it so the array stops when it reaches null values?

When I attempted that, it wouldn't even run the array. the i value would return zero.

    while (infile.hasNextLine()) {
      if (MakeList[i] == null) {
            break;
        }
        MakeList[i] = new Occupations(inCOS, inTITLE, inEMPLOY, inSALARY);
        i++;
    }

System.out.println(i);

Please and thank you for any assistance and sorry if the solution is somewhere and I didn't see it. The class opens with me creating the object array of 1000, if anyone was confused. public class OccupationsList { Occupations[] MakeList = new Occupations[1000];

Alternatively I attempted to change the code to this instead after being made aware that I may have been making the objects wrong.

   while (infile.hasNextLine()) {
        inCOS = infile.nextLine();
        inTITLE = infile.nextLine();
        inEMPLOY = Integer.parseInt(infile.nextLine());
        inSALARY = Integer.parseInt(infile.nextLine());
        MakeList[i] = new Occupations(inCOS, inTITLE, inEMPLOY, inSALARY);
        i++;
    }

This just made NumberFormatExceptions though. If the text file has commas in the number, does it consider it a int or a string? lol An example of the text file is something like this

11-3051 Industrial Production Managers 181,310 113,370 Repeated, so next line would be 12-3041

Final fixed code (only one class was edited to fix the coding, that being the loadArray. I changed infile.nextLine to infile.hasNext)

public void loadArray() throws IOException {
    String inCOS = "";
    String inTITLE = "";
    String inEMPLOY = "";
    String inSALARY = "";
    java.io.File file = new java.io.File("C:\Users\Jarod\Desktop\occupations.txt");
    Scanner infile = new Scanner(file);
    while (infile.hasNext()) { //is a boolean, is true
        inCOS = infile.nextLine();
        inTITLE = infile.nextLine();
        inEMPLOY = infile.nextLine();
        inSALARY = infile.nextLine();
        MakeList[i] = new Occupations(inCOS, inTITLE, inEMPLOY, inSALARY);
        i++;
    }
}
}
question from:https://stackoverflow.com/questions/65875893/reading-a-text-file-of-unknown-length-into-an-array-of-objects-without-arraylist

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...