#need help using a reader

108 messages · Page 1 of 1 (latest)

low lantern
#

hi there everyone, so ive got an assignment that involves reading different text files to make the different game grids. i need some help with using the reader

shadow valleyBOT
#

This post has been reserved for your question.

Hey @low lantern! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

low lantern
#

oh no

#
1
_____
1
1
_____
ab
_____
ab
_____
 w o
p b r
 w w
_____
2-1,3
#

thats what the text file looks like

#

how do i read and separate by sections

#

i was looking at the javadocs for a reader but couldnt find something like that?

#

would the best way to do this be getting everything into an array

#

turning the array into a string using .stream

#

and then getting substrings split at the dashed lines?

#

or just join actually

jagged moon
#

consider using Files.readAllLines then split by _____

low lantern
#

we are using an older version of java

jagged moon
#

how old

low lantern
#

ill find the vers

jagged moon
#

in case you're using java 8

low lantern
#

oh wait

#

we are using java 17

#

lmao

#

my last assignment was auto failed because i used java 17

#

i guess they updated

#

we used java 11 last time

jagged moon
#

what do youw ant it to look like at the end

#

or what are you trying to do withthe file i guess

low lantern
#

its really complicated

#

1 sec

#

i think u can open that in an internet explorer

#

they made like a javadoc website

#

but pretty much

#

its making a hexagon grid system with conveyor belts

#

oh also

#

this is the method

#

the automarker will run the function with a reader

#

so i dont think i can use File?

jagged moon
#

well you can use FileReader

#

which is a kind of Reader

#

or InputStreamReader, which will read from an InputStream

low lantern
#

no i mean

#

i dont know what type of reader it is going to be

jagged moon
#

so do you have to implement load or is that something you call?

low lantern
#

we implement load

jagged moon
#

oh ok you're saying how do i grab the contents of that file from Reader

low lantern
#

yep

jagged moon
#

right

low lantern
#
CharBuffer charsRead = CharBuffer.allocate(2048);
        reader.read(charsRead);
        charsRead.toString();
#

i was thinking smth along these lines

#

toString not join actually

#

and then get substrings?

jagged moon
#

so read returns -1 when it's finished

#

so how about appending to a list of chars then converting that to a string

low lantern
#
        CharBuffer charsRead = CharBuffer.allocate(2048);
        reader.read(charsRead);
        String text = charsRead.toString();
        String parts[] = text.split("_____");
jagged moon
#

but what if there are more than 2048

low lantern
#

i googled that people do multiples of 1024 normally

#

but i didnt know how to fix

#

find the number of characters

#

other than reading until it ends

#

but then it returns -1

#

rather than the number read

#

and i thought that having a counter seems inefficient

jagged moon
#

./run java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MyTest {
    public static void main(String[] args) throws IOException {
        InputStreamReader x = new InputStreamReader(new ByteArrayInputStream("askldjalskd".getBytes()));
        StringBuilder sb = new StringBuilder();
        int i = x.read();
        while (i != -1) {
            sb.append((char) i);
            i = x.read();
        }
        System.out.println(sb);
    }
}
untold magnetBOT
#

Here is your java(15.0.2) output @jagged moon

askldjalskd
jagged moon
#

x is a Reader in this example^

#

changed var to actual types to be a bit clearer

low lantern
#

but my issue is what if they dont feed in an input stream reader

#
        char[] charsRead = new char[1];
        reader.read(charsRead);
        String text = charsRead.toString();
        String parts[] = text.split("_____");
#

okay not 1 for array length

#

is each character 1 byte?

jagged moon
#

it doesn't have to bea n inputstreamreader

#

it can be any Reader

#

./run java


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class MyTest {
    public static void main(String[] args) throws IOException {
        Reader x = new InputStreamReader(new ByteArrayInputStream("askldjalskd".getBytes()));
        StringBuilder sb = new StringBuilder();
        int i = x.read();
        while (i != -1) {
            sb.append((char) i);
            i = x.read();
        }
        System.out.println(sb);
    }
}
untold magnetBOT
#

Here is your java(15.0.2) output @jagged moon

askldjalskd
jagged moon
#

updated example to just use Reader to clarify^

jagged moon
low lantern
#
        char[] charsRead = new char[2048];
        reader.read(charsRead);
        String text = charsRead.toString();
        String parts[] = text.split("_____");
#

@jagged moon does my code work?

#

also im a bit confused with yours

#

how are you turning i from int to char

#

and its returning a letter

jagged moon
low lantern
#

ahhh

#

gotcha

#

so if u cast an int to char

jagged moon
low lantern
#

itll turn into a letter

#

thats cool

jagged moon
#

yes

low lantern
#

like appending each character into the string

jagged moon
#

ah yeah that's right

low lantern
#

also would my code work?

#

or would urs just be a better way

#

(assuming its less than 2048 char long)

#

actually i need to use join()

#

but like does my code logic work at all?

#
        StringBuilder fileText = new StringBuilder();
        int textChar = reader.read();
        while (textChar != -1) {
            fileText.append((char) textChar);
            textChar = reader.read();
        }
        String parts[] = fileText.toString().split("_____");
jagged moon
low lantern
#

also I'm rly stupid

#

i couldve just made a buffered reader

jagged moon
#

true

low lantern
#

sorry about that aha

#

i just wasted ur time lmao