Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom delim for tokenizer #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 58 additions & 44 deletions Kattio.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
* a good idea to use hasMoreTokens() to check for end-of-file.
*
* @author: Kattis
*
* Modified based on MIT License
* @author: Shear
*/

import java.util.StringTokenizer;
Expand All @@ -37,58 +40,69 @@
import java.io.OutputStream;

class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}

public boolean hasMoreTokens() {
return peekToken() != null;
}
public Kattio(InputStream i, String delim){
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
this.delim = delim;
}

public int getInt() {
return Integer.parseInt(nextToken());
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}

public double getDouble() {
return Double.parseDouble(nextToken());
}
public boolean hasMoreTokens() {
return peekToken() != null;
}

public long getLong() {
return Long.parseLong(nextToken());
}
public int getInt() {
return Integer.parseInt(nextToken());
}

public String getWord() {
return nextToken();
}
public double getDouble() {
return Double.parseDouble(nextToken());
}

public long getLong() {
return Long.parseLong(nextToken());
}

public String getWord() {
return nextToken();
}

private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String delim;

private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null)
return null;
if (delim == null)
st = new StringTokenizer(line);
else
st = new StringTokenizer(line, delim);
}
token = st.nextToken();
} catch (IOException e) {
}
return token;
}

private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}