If I’m taking input with a scanner and I want to put a single character into a char type variable, what is the most efficient algorithm? I just noticed that both next() and nextLine() return strings regardless of the size, and aside from getting a string, casting it into a char array and then taking the first element, I can’t think of how I would do this. There must be a more efficient way!
Share
You could use
FileChannelto create aByteBuffer. Once here you can then call theasCharBuffer()method to return a char buffer.Here’s a decent example:
Example using CharBuffers