I need to write my Scanner. The problem is that if I enter data from the console into IDEA as follows:
Input: "1 2^D" Output: "" What should output: "1 2"
Input: "1 2 3 4 5 6 7" Output: "1 2 3 4 5 6" What should output: "1 2 3 4 5 6 7"
What could be the problem? And if you suddenly know how you can fix it? Code:
import java.io.*;
import java.util.Arrays;
public class OwnScanner {
private final Reader reader;
private final char[] Buffer = new char[1024];
private int currIndexInBuffer = 0;
private int size = 0;
private char symbol;
private final char charNegOne = ((char) -1);
private final StringBuilder outputBuilder = new StringBuilder();
public OwnScanner(String toRead) {
this.reader = new StringReader(toRead);
}
public OwnScanner(InputStream toRead) {
this.reader = new InputStreamReader(toRead);
}
public OwnScanner(FileInputStream toRead) {
this.reader = new InputStreamReader(toRead);
}
private char nextChar() throws IOException {
if (currIndexInBuffer >= size) {
currIndexInBuffer = 0;
size = reader.read(Buffer, 0, 1024);
}
if (size == -1) {
return charNegOne;
} else {
return Buffer[currIndexInBuffer++];
}
}
private boolean skipWhitespaceAndLetter() throws IOException {
while ((symbol = nextChar()) != charNegOne) {
if (Character.isDigit(symbol)) {
currIndexInBuffer--;
return true;
}
}
return false;
}
private boolean skipWhitespaceAndDigit() throws IOException {
while ((symbol = nextChar()) != charNegOne) {
if (!(Character.isDigit(symbol)) & !(Character.isWhitespace(symbol))) {
currIndexInBuffer--;
return true;
}
}
return false;
}
public boolean hasNext() throws IOException {
return skipWhitespaceAndDigit();
}
public boolean hasNextInt() throws IOException {
return skipWhitespaceAndLetter();
}
public char nextSym() throws IOException {
return nextChar();
}
public void close() throws IOException {
reader.close();
}
public boolean hasNextLine() throws IOException {
int startIndex = currIndexInBuffer;
while ((symbol = nextChar()) != charNegOne) {
if (System.lineSeparator().contains(String.valueOf(symbol))) {
currIndexInBuffer = startIndex;
return true;
}
}
return false;
}
public String next() throws IOException {
outputBuilder.setLength(0);
skipWhitespaceAndDigit();
while ((symbol = nextChar()) != charNegOne) {
if (Character.isDigit(symbol) | Character.isWhitespace(symbol)) {
break;
}
outputBuilder.append(symbol);
}
return String.valueOf(outputBuilder);
}
public String nextLine() throws IOException {
outputBuilder.setLength(0);
while ((symbol = nextChar()) != charNegOne) {
if (System.lineSeparator().contains(String.valueOf(symbol))) {
break;
}
outputBuilder.append(symbol);
}
return String.valueOf(outputBuilder);
}
public int nextInt() throws IOException {
outputBuilder.setLength(0);
skipWhitespaceAndLetter();
while ((symbol = nextChar()) != ((char) -1)) {
if (Character.isLetter(symbol) | Character.isWhitespace(symbol)) {
break;
}
outputBuilder.append(symbol);
}
return Integer.parseInt(String.valueOf(outputBuilder));
}
public static void main(String[] args) throws IOException {
OwnScanner sc = new OwnScanner(System.in);
while (sc.hasNextLine()) {
System.out.println(sc.nextInt());
}
}
}
It seems to me that the error may be in skipWhitespaceandLettre or skipWhitespaceandDigit, but I'm not sure.