How to Read Input Character-By-Character in Java

How do I read input character-by-character in Java?

Use Reader.read(). A return value of -1 means end of stream; else, cast to char.

This code reads character data from a list of file arguments:

public class CharacterHandler {
//Java 7 source level
public static void main(String[] args) throws IOException {
// replace this with a known encoding if possible
Charset encoding = Charset.defaultCharset();
for (String filename : args) {
File file = new File(filename);
handleFile(file, encoding);
}
}

private static void handleFile(File file, Charset encoding)
throws IOException {
try (InputStream in = new FileInputStream(file);
Reader reader = new InputStreamReader(in, encoding);
// buffer for efficiency
Reader buffer = new BufferedReader(reader)) {
handleCharacters(buffer);
}
}

private static void handleCharacters(Reader reader)
throws IOException {
int r;
while ((r = reader.read()) != -1) {
char ch = (char) r;
System.out.println("Do something with " + ch);
}
}
}

The bad thing about the above code is that it uses the system's default character set. Wherever possible, prefer a known encoding (ideally, a Unicode encoding if you have a choice). See the Charset class for more. (If you feel masochistic, you can read this guide to character encoding.)

(One thing you might want to look out for are supplementary Unicode characters - those that require two char values to store. See the Character class for more details; this is an edge case that probably won't apply to homework.)

Take a char input from the Scanner

You could take the first character from Scanner.next:

char c = reader.next().charAt(0);

To consume exactly one character you could use:

char c = reader.findInLine(".").charAt(0);

To consume strictly one character you could use:

char c = reader.next(".").charAt(0);

How to take character input in java

use the System class

char yourChar = System.in.read()

Reading a character input in the console using the Scanner Class in Java

This method Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

char ch = sc.next(".").charAt(0);

In this code it waiting to match this from this input "."

Reading a single char in Java

You can either scan an entire line:

Scanner s = new Scanner(System.in);
String str = s.nextLine();

Or you can read a single char, given you know what encoding you're dealing with:

char c = (char) System.in.read();

How to read an input file char by char using a Scanner?

You can convert in an array of chars.

import java.io.*;
import java.util.Scanner;

public class ScanXan {
public static void main(String[] args) throws IOException {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("yourFile.txt")));
while (s.hasNext())
{
String str = s.next();
char[] myChar = str.toCharArray();
// do something
}
} finally {
if (s != null) {
s.close();
}
}
}

How to read characters in a string in java

You only want to next() your reader once, unless it has a lot of the same toke nrepeated again and again.

String nodes = reader.next();
for(int i = 0; i < nodes.length(); i++) {
System.out.println(nodes.charAt(i));
}

Fast read text file character-by-character(java)

I would use BufferedReader, it reads buffered. A short sample:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.CharBuffer;

public class Main {
public static void main(String... args) {
try (FileReader fr = new FileReader("a.txt")) {
try (BufferedReader reader = new BufferedReader(fr)) {
CharBuffer charBuffer = CharBuffer.allocate(8192);
reader.read(charBuffer);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

The default constructor uses a default buffersize of 8192. In case you want to use a different buffer size you can use this constructor. Alternatively you can read in an array buffer:

 ....
char[] buffer = new char[255];
reader.read(buffer);
....

or read one character at a time:

int char = reader.read();

Character by character reading the input from JEditorPane in Java

So I am not getting how and which function to use to read character from JEditorPane as soon as user inputs into the JEditorPane.

You can use a DocumentListener Read the section from the Swing tutorial on How to Write a DocumentListener for more information and examples.

If you are creating an editor, which just displays the text, not the actual formatting, then you should use a JTextArea or a JTextPane. A JEditorPane is really only for displaying existing HTML files.



Related Topics



Leave a reply



Submit