#InputStreamer, how does it know when it is a newline

1 messages · Page 1 of 1 (latest)

uneven crag
#

So I have this funciton: ```java
private static String readInput(InputStream f) throws java.io.IOException {
Reader stdin = new InputStreamReader(f);
StringBuilder buf = new StringBuilder();
char input[] = new char[1024];
int read = 0;
while ((read = stdin.read(input)) != -1) {

        buf.append(input, 0, read);          
    }                                              
    return buf.toString();                        
}```

After using functino I am analysing the string I get but I never ever match anything with "\n" why?

sonic juniperBOT
#

<@&987246399047479336> please have a look, thanks.

uneven crag
#

I meant InputStreamReader btw

plain dew
#

also wdym, why are you talking about newline ?

uneven crag
uneven crag
# plain dew also wdym, why are you talking about newline ?

What I mean is given a text file Hey how are you I am find you can see that there are two lines which are separated byt the whitespace \n. The string I get from this function contains it which I have verified using contains but now I just can't seem to use regex to pattern match it.

#

I have like ```java
Pattern pattern = Pattern.compile("0x[a-fA-F0-9]{6}|[A-Za-z]+|[0-9]+|%|\n|\.|"|\s*");
Matcher match = pattern.matcher(input);
if(match.group().equals("\n")){
tokens.add(new Token(TokenType.NEWLINE));
}

sonic juniperBOT
pure sentinel
#

Why not just use Scanner. And why do you even care about \n if you are appending it to one String regardless

plain dew
pure sentinel
pure sentinel
#

Wp

uneven crag
pure sentinel
plain dew
uneven crag
#

even if I used bufferreader I'd still need to pattern match "\n"

plain dew
pure sentinel
#

readLine();

plain dew
uneven crag
plain dew
#

and please handle the files correctly

#

with a try with resources

uneven crag
#

wait a minute agian

#

I will try to use bufferreader which I think will be a lot easier honestly but rn the string I get is totally fine tho

plain dew
uneven crag
#

Why can't I just pattern match \n?

plain dew
#

why are you trying to do something complex when you can do something simple ?

uneven crag
plain dew
#

You can't match a language with a regex

#

you have to make a lexer

uneven crag
# plain dew you have to make a lexer

Yes it is inside my lexer. I am not really sure I understand what you mean. I read a file in my lexer class then I put my keywords/ Tokens I match using regex inside a list. Then I make a parse tree..

plain dew
#

ah right