#Get Input Stream of Characters?!

1 messages · Page 1 of 1 (latest)

digital summit
#

Is it possible to have an input stream of characters (such that I'm iterating one character at a time, wher the logic is the exact same as iterating line by line)?

cobalt violetBOT
#

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

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

Yes, it is possible to have an input stream of characters in Java. You can use the Reader class to read characters from an input source.

Here's an example of how you can create an input stream of characters and iterate over them one character at a time:

import java.io.*;

public class CharacterInputStreamExample {
    public static void main(String[] args) {
        try {
            // Create a FileReader to read characters from a file
            Reader reader = new FileReader("input.txt");

            // Create a BufferedReader for efficient reading
            BufferedReader bufferedReader = new BufferedReader(reader);

            int character;
            while ((character = bufferedReader.read()) != -1) {
                // Process each character here
                char c = (char) character;
                System.out.print(c);
            }

            // Close the reader
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we use a FileReader to read characters from a file. The BufferedReader wraps the FileReader for efficient reading. The read() method of the BufferedReader returns the next character as an integer value, which we then cast to a char.

You can replace "input.txt" with any other source of characters like a network socket or standard input (System.in) depending on your requirements.

dawn bobcat
#

what are you starting with?

#

char array?

#

list of char?

#

string?

digital summit
#

I'm starting with a Scanner of a txt file

#

I want to essentially iterate over it character by character, like a film tape over characters

#

that being said, the chatgpt answer might be good

#

I'mma try that

limber chasm
#

Its better if you use NIO

#

As specially the Files class

#

Then you can get all lines in just one go without FileReader, BufferedReader etc

digital summit
#

but I need an api that goes through character by character like a film tape

#

where I don't have to iterate over lines and then iterate over characters

#

it makes my logic more complex than it needs to be

limber chasm
#

Of course based on what u plan to do with the chars

#

With flatMap you can combine all lines into one and then do something with it

#

Arrays.stream(lines).flatmap(...).foreach(...);

digital summit
#

I had a post about for each before

#

it's really shitty to work with when you want to mutate state from parent scope

#

it just doesn't allow you to do it

#

which makes it unusable in my situation

dawn bobcat
#

the solution is better

#

and you are not forced to use forEach

digital summit
#

wym

#

I can't use it if I can't mutate state from parent scope

dawn bobcat
#

use toList on the Stream and then iterate over it using a normal for loop

#

that way you wont have the problem with lambda

digital summit
#

I know, but that defeats the efficiency of a stream

#

there's a clear performance loss when converting to a list

dawn bobcat
#

that pre optimization

#

which is bad

#

the difference will be so small

digital summit
#

for files that are potentially hundreds of thousands of lines long, it would blow up

dawn bobcat
#

that you shouldnt care at all

digital summit
#

it's not premature optimization it's trying to be aware of java's strenghts and weaknesses

#

in rust the default way I'd do things IS the efficient way

#

it's not premature optimization if its more convenient as well

dawn bobcat
#

but we are not in rust

tropic dome
#

You have no idea if it would actually cause problem or if it won't be noticable at the first place

#

and you haven't explained your use case, why do you want to do that at the first place ?

digital summit
#

@tropic domehttps://discord.com/channels/272761734820003841/1198302493583614012