Regular Expressions and String.replaceAll()
Hello! This is my first post, so just trying to see how this turns out...
I am writing an NBT (Named Binary Tag) reader. I would like to remove all the double newlines. So:
will be
Here is my code (I am using the JNBT library, look it up if you want it):
IO.java
Code:
import java.io.*;
public class IO {
public String strIn() {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String input = null;
try {
input = console.readLine();
}
catch (IOException e) { input = "<" + e + ">"; }
return input;
}
public double numIn()
{
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String input = null;
try {
input = console.readLine();
}
catch (IOException e) { input = "<" + e + ">"; }
return Double.parseDouble(input);
}
public void out(String ut) {
System.out.println(ut);
}
public void out(int ut) {
String go = Integer.toString(ut);
out(go);
}
public void out(double ut) {
String go = Double.toString(ut);
out(go);
}
}
NBT.java
Code:
import org.jnbt.*;
import java.io.*;
public class NBT {
public static void main(String[] args) {
// Terminal input
IO io = new IO();
// Get Directory and Filename of file
NBTInputStream in = null;
System.out.println("Directory:");
String dir = io.strIn();
System.out.println("File:");
String file = io.strIn();
File f = new File(dir,file);
// Get whole data
try {
in = new NBTInputStream(new FileInputStream(f));
}
catch (IOException e) {
System.out.println("Opps...");
System.exit(0);
}
Tag t = null;
try {
t = in.readTag();
}
catch (IOException e) {
System.out.println("ARG! "+e);
System.exit(0);
}
String whole = t.toString();
// Excess stuff Removal
whole = whole.replaceAll("[{]", "");
whole = whole.replaceAll("[}]", "");
whole = whole.replaceAll("[ ]", "");
whole = whole.replaceAll("entries", "");
whole = whole.replaceAll("oftype", "");
// Here is where I am stuck!
whole = whole.replaceAll("$\n", "\n");
String[] line = null;
// Split whole data into lines
line = whole.split("\n");
// Print out whole data
System.out.println(whole);
int x = 0;
// Split lines into definers and data
String[][] data = new String[java.lang.reflect.Array.getLength(line)][];
while (x<java.lang.reflect.Array.getLength(line)) {
data[x]=line[x].split("[:]");
x=x+1;
}
System.out.println(data[2][0]);
System.out.println(data[2][1]);
}
}