#need help using a reader
108 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @low lantern! Please use
/closeor theClose Postbutton 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.
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
consider using Files.readAllLines then split by _____
declaration: module: java.base, package: java.nio.file, class: Files
.
we are using an older version of java
how old
ill find the vers
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
what do youw ant it to look like at the end
or what are you trying to do withthe file i guess
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?
well you can use FileReader
which is a kind of Reader
or InputStreamReader, which will read from an InputStream
so do you have to implement load or is that something you call?
we implement load
oh ok you're saying how do i grab the contents of that file from Reader
yep
right
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?
so read returns -1 when it's finished
so how about appending to a list of chars then converting that to a string
CharBuffer charsRead = CharBuffer.allocate(2048);
reader.read(charsRead);
String text = charsRead.toString();
String parts[] = text.split("_____");
but what if there are more than 2048
yea this is what i was worried about
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
./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);
}
}
Here is your java(15.0.2) output @jagged moon
askldjalskd
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?
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);
}
}
Here is your java(15.0.2) output @jagged moon
askldjalskd
updated example to just use Reader to clarify^
yes
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
casting (a char is an int in disguise)
it's returning the whole passed-in Reader's contents
yes
but one by one right?
like appending each character into the string
ah yeah that's right
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("_____");
yeah this is pretty much what i have but you're splitting by underscores atthe end
true