#Python dev crying for help over reading file into array

1 messages · Page 1 of 1 (latest)

jagged sinew
#

How do I read a file into an array with Java using only the Scanner class? I'm not given the length of the file and am only allowed to use the Scanner class for this task, no more modern APIs or anything.

// load in file lines/content into array
    public static String[] loadFile(String fname) {
        Scanner fileIn = null;
        
        try {
            fileIn = new Scanner(new File(fname));
        } catch (FileNotFoundException e) {
            String[] emptyArr = new String[0];
            return emptyArr;
        }
        
        int lineCount = 0;
        
        while(fileIn.hasNextLine()) {
            lineCount++;
            fileIn.nextLine();
        }
        
        String[] fileContent = new String[lineCount];
        
        
        
        fileIn.close();
        return fileContent;
    }``` This is all I have so far, not sure if this is down the right path
exotic lakeBOT
#

Hey, @jagged sinew!
Please remember to /close this post once your question has been answered!

sharp briar
#

The idea would be to use an ArrayList, not an array

jagged sinew
#

yeah here's teh thing ive learned arraylists before but

#

school cs is stupid

#

and they wont let me use it until we cover it

#

@sharp briar im forced to use a normal array for school purposes

#

😭

#

bruh if i show my school teacher some ml/ai shit hes gonna know whos the boss

#

oh well

sharp briar
#

I don't know what to tell you. Store the file in an array of size 50000 or something

#

Then copy it over to an array of the exact right size

jagged sinew
#

he told us we couldnt do that 😦

#

yeah again school cs is new levels of stupid

sharp briar
#

Maybe they just want you to redo ArrayList

jagged sinew
#

not sure

sharp briar
#

Start with an array of size 1. If the array is ever too small, copy it over to a newly created array of double the size

jagged sinew
sharp briar
#

What? No you're finished reading the file

jagged sinew
#

right

#

but in the loop

#

i kinda dumb u see

sharp briar
#

Were you not a Python dev?

jagged sinew
#

i am but uhh id ont pay attention in class for java

sharp briar
#

It doesn't matter. Reading a file is reading a file

jagged sinew
#

alr

#

yeah i mean im assuming he doesnt want us to implement arraylist manually

#

since we shouldnt "know what that is"

sharp briar
#

But it's possible to be clever enough to realize that when an array isn't big enough, you could make a bigger array and copying over

jagged sinew
#

that's true

sharp briar
#

Anyway, I wasn't in your class so I can't guess what was the vibe of what is allowed, what isn't, and what they thought you could figure out

jagged sinew
#

gotcha

#

alr ill just do the large array trick

#

willing to sacrifice a bit of my grade for good code

#

even if the teacher doesnt agree

vague maple
#

well

#

you could just do it without a buffer

#
  1. Read the entire file into one string variable
  2. Split the var by your separator
  3. Return the result
woven kindle
#

Kinda stupid

#

Arraylist literally better

vague maple
#

Arrays are always more efficient if you know the fixed size

#

Which you do when reading it all at once

maiden karma
#

Just use system.arraycopy