Problem with own Scanner's methods

I need to realise simple version of scanner for much faster reading. I must use block reading available from Reader and its buffer. I can't get how to make hasNext methods (at least hasNext and hasNextInt) due to the problem, that if next appropriate answer is lay in some buffers and input is keyword I will lost intermediate buffers as i cant reread system input. also as i understand using linkedhashmap or String for intermediate buffers can cost a lot of memory if answer is lay in a few million buffers. can someone tell it is more sensible to realise these methods i have such realisation now(sorry if someone thinks that i have bad or cumbersome code, i m a newbie in java)
public class MyScanner {
private char[] buffer = new char[4096];
private Reader in;
private int position = 0;
private int read = 0;

public MyScanner(String inputFile) throws IOException {
this.in = new InputStreamReader(
new FileInputStream(inputFile), "utf8"
);
}

public MyScanner(InputStream input) {
this.in = new InputStreamReader(input);
}

private String getNext(String token) {
StringBuilder chars = new StringBuilder();
StringBuilder remainder = new StringBuilder();
if (position == 0) {
read = in.read(buffer);
}
while (read >= 0) {
..... //logic of checking symbols
}
public class MyScanner {
private char[] buffer = new char[4096];
private Reader in;
private int position = 0;
private int read = 0;

public MyScanner(String inputFile) throws IOException {
this.in = new InputStreamReader(
new FileInputStream(inputFile), "utf8"
);
}

public MyScanner(InputStream input) {
this.in = new InputStreamReader(input);
}

private String getNext(String token) {
StringBuilder chars = new StringBuilder();
StringBuilder remainder = new StringBuilder();
if (position == 0) {
read = in.read(buffer);
}
while (read >= 0) {
..... //logic of checking symbols
}
10 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @flechergame! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant 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.
dan1st
dan1st2mo ago
Depending on what you for your hasNext methods, you may not need a large buffer between methods and if you need a bigger buffer locally (within one method), that can likely be optimized (at least if the method is used very frequently)
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
KnightElf
KnightElf2mo ago
Hi, My name is Lukas and I'm proficient in Java. I read your post and I think I can help you.
KnightElf
KnightElf2mo ago
check this code
KnightElf
KnightElf2mo ago
I hope this can help you
flechergame
flechergameOP2mo ago
wow, it is almost ideally what i need, i will try to add checking special word patterns in it, thank you a lot, I can't contain my joy
JavaBot
JavaBot2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
KnightElf
KnightElf2mo ago
You're welcome is it working?
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?