I have a problem.
I want to make a programm, that takes a Log File and parse it line for line.
A Log looks like this:
"2014-02-14 14:26:37,836 INFO [org.jboss.msc] (main) JBoss MSC
version 1.0.4.GA-redhat-1"
My code:
public static void main(String[] args) throws InterruptedException {
try
{
String sCurrentLine;
BufferedReader br = new BufferedReader(new FileReader("C:\\server.log"));
while ((sCurrentLine = br.readLine()) != null) {
String datetime = "";
String level = "";
String category = "";
String message = "";
String output = "";
if(sCurrentLine.length()<1){
}
else{
if (sCurrentLine.charAt(4)=='-' && sCurrentLine.charAt(7)=='-' && sCurrentLine.charAt(13)==':' && sCurrentLine.charAt(16)==':'){
String[] leerzeichen = sCurrentLine.split(" ");
String[] leerzeichenz = sCurrentLine.split(" ");
datetime = leerzeichen[0] + " " + leerzeichen[1];
level = leerzeichen[2];
category = leerzeichen[4];
int arraylength = leerzeichen.length;
for (int l=5; l<arraylength; l++){
message = message.concat(leerzeichen[l] + " ");
}
output = datetime + level + category + message;
} else {
message = message.concat(sCurrentLine);
output += message;
}
}
System.out.println(output);
}
} catch (IOException e) {
e.printStackTrace();
}
}
The Program looks, if the beginning of the Line looks like this: 0000-00-00 00:00:00
If not, the Line is only a message in connection with the line before.
But I always get the Error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:658)
at javaapplication5.JavaApplication5.main(JavaApplication5.java:38)
But he reads some lines of the log. But not until the end of the log.
Can you help me? sorry for my English.
You have problem in line:
if (sCurrentLine.charAt(4)=='-' && sCurrentLine.charAt(7)=='-' && sCurrentLine.charAt(13)==':' && sCurrentLine.charAt(16)==':'){
You cannot assume that the line have at least 4, 7 or more characters.
Try changing it to this:
if (sCurrentLine.length() >= 16 && sCurrentLine.charAt(4)=='-' && sCurrentLine.charAt(7)=='-' && sCurrentLine.charAt(13)==':' && sCurrentLine.charAt(16)==':'){
The cause of error is quite straight forward; you first check if string has length > 1, and then directly ask for char at index 4 -- which is no guarantee to exists since all you know is thats length > 1.
Hence, add proper checking before trying to access char at index 4, and most of all, as said in comment by #VusP, add some print/debugging of the read string before trying to parse it.
Related
I want to get parse (if possible) the partition that contains the string that is a number(i.e."95") but I could accept any strategy to do it. my code works for hashMaps
to not make this lengthy, this is how the lines in the input file look like:
Kostas_Antetokounmpo 37
public static void main (String[] args) throws IOException {
String path = "roster.txt";
String row;
Integer row1;
HashTable_NBA<Integer,String> roster = new HashTable_NBA<>();
BufferedReader read = new BufferedReader(new FileReader(path));
while ((row = read.readLine()) != null){
String[] partition = row.split(" ", 2);
if(partition.length >= 2){
Integer key = Integer.parseInt(partition[1]);
String value = partition[0];
roster.put(key, value);
}
}
System.out.println(roster);
}
}
//EDIT
//the errors are these
Exception in thread "main" java.lang.NumberFormatException: For input string: "37 "
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at HashTable_NBA.main(HashTable_NBA.java:161)
This is a guess but I am assuming there could be more than one space between the values. And I remove the 2 from the split command. That was causing problems.
while ((row = read.readLine()) != null){
String[] partition = row.split("\\s+");
if(partition.length >= 2){
Integer key = Integer.parseInt(partition[1]);
String value = partition[0];
roster.put(key, value);
}
}
I'm working on a Java assignment for school. The assignment is to deal with 2 files reading the first, using the second to make adjustments on the first, and finally, outputing into a new file.
Scanner inputRecords = new Scanner(new File("Records.txt"));
Scanner inputTransactions = new Scanner(new File("Transactions.txt"));
BufferedWriter outputFile = (new BufferedWriter(new FileWriter("NewRecords.txt", true)));
char code; // transaction code
String lineTran = "";
String lineRecord = "";
String midS = "";
String tidS = "";
int tid = 0, mid= 0;
for (int x = 0; x < 6; x++)
{
lineTran = inputTransactions.nextLine();
code = lineTran.charAt(0);
System.out.println(code);
tidS = lineTran.substring(2,11);
tid = Integer.parseInt(tidS);
lineRecord = inputRecords.nextLine();
midS = lineRecord.substring(0,9);
mid = Integer.parseInt(midS);
if (mid < tid) // add a new record lineTran.substring(2,lineTran.length()
{
outputFile.write(lineRecord);
outputFile.newLine();
lineRecord = inputRecords.nextLine();
}
else if (mid == tid )
{
if (code == 'C') //change record
{
outputFile.write(lineTran.substring(2,lineTran.length()));
outputFile.newLine();
lineTran = inputTransactions.nextLine();
}
else if (code == 'D') //delete record
{
lineTran = inputTransactions.nextLine();
lineRecord = inputRecords.nextLine();
}
else // add error
{
System.out.println(lineRecord + " Already Exists...add error");
lineTran = inputTransactions.nextLine();
}
}
else
{
if (code == 'A') // add record
{
outputFile.write(lineTran.substring(2,lineTran.length()));
outputFile.newLine();
lineTran = inputTransactions.nextLine();
}
else if (code == 'C') // change error
{
System.out.println(lineRecord + " Record already exist...Change Error");
lineTran = inputTransactions.nextLine();
}
else // delete error
{
System.out.println(lineRecord + " Record does not exist...delete error");
lineTran = inputTransactions.nextLine();
}
}
Note that:
Records.txt has 10 lines of information (example: ######### lastname firstname occupation)
Transactions.txt has 6 lines of information (example: 'A,D,or C' ######### lastname firstname occupation)
The issue I'm having is no matter the type of loop i run i reach one of 2 deadends.
1) in the case of the for loop above
D
A
C
A
C
386326383 Slim Kan personalTrainer No changes were found...Change Error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at fileHandling.main(fileHandling.java:26)
is the outcome and nothing writen to file.
2) If I run for loop through x<5 program runs fine and, however, skips the last transaction.
I tried "do while" and "while" loops but only got similar results. any suggestions?
On the if condition you have assign lineTran = inputTransactions.nextLine(); then on the top of for loop you assign it again. Means that you are dismiss 1 line every loop. So that, in transaction file you have 6 lines, then you loop will run more loop than the number of line in the file because of you have read .nextLine() more than the number of lines.
I am facing a problem in the following code. I am trying to run the program and it terminates when it hits empty space in my input. How else I should approach this.
try {
BufferedReader sc = new BufferedReader(new FileReader(text.txt);
ArrayList<String> name = new ArrayList<>();
ArrayList<String> id = new ArrayList<>();
ArrayList<String> place = new ArrayList<>();
ArrayList<String> details = new ArrayList<>();
String line = null;
while ((line = sc.readLine()) !=null) {
if (!line.trim().equals("")) {
System.out.println(line);
if (line.toLowerCase().contains("name")) {
name.add(line.split("=")[1].trim());
}
if (line.toLowerCase().contains("id")) {
id.add(line.split("=")[1].trim());
}
if (line.toLowerCase().contains("location")) {
place.add(line.split("=")[1].trim());
}
if (line.toLowerCase().contains("details")) {
details.add(line.split("=")[1].trim());
}
}
}
PrintWriter pr = new PrintWriter(new File(text.csv));
pr.println("Name;Id;;Location;Details");
for (int i = 0; i < name.size(); i++) {
pr.println(name.get(i) + ";" + id.get(i) + ";" + place.get(i) + ";" + details.get(i));
}
pr.close();
sc.close();
} catch (Exception e) {
e.printStackTrace();
} }
My Input looks like
name = abc
id = 123
place = xyz
details = hsdyhuslkjaldhaadj
name = ert
id = 7872
place =
details = shahkjdhksdhsala
name = sfd
id = 4343
place = ksjks
Details = kljhaljs
when im trying to execute then above text my program terminates at place = "null" because of no value there.I need the output as an empty space created in place ="null" and print the rest as follows in a .csv file
If you process the location, line.split("=")[1] could result in an ArrayIndexOutOfBoundException and line.split("=")[1].trim() could result in a NullPointerException.
You can avoid this by testing your parsed result.
Instead of place.add(line.split("=")[1].trim());, do place.add(parseContentDefaultEmpty(line));, with:
private String parseContentDefaultEmpty(final String line) {
final String[] result = line.split("=");
if(result.length <= 1) {
return "";
}
final String content = line.split("=")[1];
return content != null ? content.trim() : "";
}
First there is a issue,your input file contains key as "place" but your are trying for word "location"
if (line.toLowerCase().contains("location")) { //this must be changed to place
place.add(line.split("=")[1].trim());
}
Modified the code snippet as below.check it
while ((line = sc.readLine()) != null) {
if (!line.trim().equals("")) {
System.out.println(line);
if (line.toLowerCase().contains("name")) {
name.add(line.split("=")[1].trim());
}
if (line.toLowerCase().contains("id")) {
id.add(line.split("=")[1].trim());
}
if (line.toLowerCase().contains("place")) {
// change done here to add space if no value
place.add(line.split("=").length > 1 ? line.split("=")[1]
.trim() : " ");
}
if (line.toLowerCase().contains("details")) {
details.add(line.split("=")[1].trim());
}
}
}
Setting question to line doesn't appear to change what line is read later (if you're wanting the line to advance before it hits the while loop).
I'm trying to write a part of an String array into a String but I just stuck at a problem.
The disposal of the value of line is always like this: "status.test.status.close.name = Closed".
The only static of this value is "status." and ".name". I just want to get the part between "status." and ".name". With the code below I get this result: "status.test.status.close". My question now is, is it possible to delete parts of an array, for example: technicalNames.delete["status."];? Or does anyone has another hint how to realize it?
public void setTechnicalName(File javaFile) throws IOException {
if(javaFile.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(javaFile));
String line = null;
while((line = reader.readLine()) != null)
if (line.contains("In approval") || line.contains("In Approval") || line.contains("In review") || line.contains("In Review") || line.contains("Closed")){
System.out.println(line);
String[] technicalNames = line.split(".name");
String technicalName = technicalNames[0];
System.out.println(technicalName);
}
reader.close();
}
}
That is the .xml file i read out:
status.test.status.close.name = Closed
status.test.status.in.approval.name = In approval
status.test.status.in.review.name = In review
test.field.approver1 = Approver
test.field.lookupworkflow =
test.field.temp = temp
Thanks in advance!
I am assuming that you are interested in parts between status and .name. You can try this way of doing it.
public static void setTechnicalName(File javaFile) throws IOException {
if(javaFile.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(javaFile));
String line = null;
int statusOffet = "status.".length();
while((line = reader.readLine()) != null){
int indexOfStatus = line.indexOf("status");
int indexOfName = line.lastIndexOf(".name");
boolean isReqLine = line.contains("In approval")
|| line.contains("In Approval")
|| line.contains("In review")
|| line.contains("In Review")
|| line.contains("Closed");
if(isReqLine && indexOfStatus != -1 && indexOfName != -1){
System.out.println(line);
String stage = line.substring(indexOfStatus + statusOffet, indexOfName);
System.out.println(stage);
}
}
reader.close();
}
}
EDIT : as per comment to match format, I have included "." when calculating offset and used indexOf
When you want to split your given line into parts, you can do it similar to
final String string = "status.test.status.close.name = Closed";
final String[] split = string.substring(0, string.indexOf("=")).split("\\.");
System.out.println("split = [" + split[0] + ", " + split[1] + ", " + split[2] + ", " + split[3] + ", " + split[4] + "]"); // split = [status, test, status, close, name ]
and pick the appropriate values out of split afterwards.
Well, you have status defined twice. So assuming you are referring to the first status, and the desired result is to print out test.status.close you could do this if you knew that indexes 0 and n-1 would be not of interest:
StringBuilder builder = new StringBuilder();
String[] parts = line.split(".name")[0].split("\\.");
for(int i = 1; i < parts.length - 1; i++){
builder.append(parts[i]);
if(i < parts.length - 1){
builder.append(".");
}
System.out.println(builder.toString())
I am trying to read a simple .CSV file and create a 2D array of Strings. Here is the array:
1,1,1,1,1,1
2,2,2,2,2,2
3,3,3,3,3,3
4,4,4,4,4,4
My code is supposed to find six columns and four rows, but it stops after the third column and moves on to the next row and I cannot figure out why this is happening.
In addition to this, it returns an out of bounds exception even though it quits early.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
Here is the code and it is followed by the output.
public String[][] ascToStringArray(String ascFileIn) {
String directory ="c:\\data\\"; // "\" is an illegal character
String[][] numbers= new String[4][6]; // 4 rows 6 columns
try{
BufferedReader Br = new BufferedReader(new FileReader(directory + ascFileIn));
String line;
int row = 0;
int col = 0;
//read each line of text file
while((line = Br.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line,",");
//Populating Columns
while (st.hasMoreTokens())
{
//get next token and store it in the array
numbers[row][col] = st.nextToken();
System.out.println(row + " " + col + " = " + st.nextToken());
col++;
}
row++;
}
//close the file
Br.close();
return numbers;
}
catch(IOException exc) {
System.out.println("Error reading file.");
return numbers;
}
}
Here is the output:
0 0 = 1
0 1 = 1
0 2 = 1
1 3 = 2
1 4 = 2
1 5 = 2
If anyone can figure out why it is quitting early and throwing me an out of bounds error no matter how large I make the array I would really appreciate it.
You are using nextToken twice.
numbers[row][col] = st.nextToken();<-1---
System.out.println(row + " " + col + " = " + st.nextToken());<--2--Skips element
But using only one value so in one row only three element of line will be added.
Reason of Exception
You are not resetting the col=0 after the execution of inner while loop which leads to ArrayIndexOutOfBound for col=6 as col size in array is 6 means 0 to 5 so will throw exception when col=6.
Firstly the system.out.println inside the inner while loop consumes a token.. Second on entring the inner while loop you should reset the cols files. to zero.
public String[][] ascToStringArray(String ascFileIn) {
String directory = "c:\\data\\"; // "\" is an illegal character
String[][] numbers = new String[4][6]; // 4 rows 6 columns
try {
BufferedReader Br = new BufferedReader(new FileReader(directory + ascFileIn));
String line;
int row = 0;
int col = 0;
// read each line of text file
while ((line = Br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ",");
col = 0;
// Populating Columns
while (st.hasMoreTokens()) {
// get next token and store it in the array
numbers[row][col] = st.nextToken();
col++;
}
row++;
}
// close the file
Br.close();
return numbers;
} catch (IOException exc) {
System.out.println("Error reading file.");
return numbers;
}
}
It's because you are calling st.nextToken() twice, swallowing an extra token in your System.out.println.
Instead, first save it into a String:
String token = st.nextToken();
then you can use the same String in your print and for the array.
http://msdn.microsoft.com/fr-fr/library/aa989865(v=vs.80).aspx
StringTokenizer.nextToken () : Gets the next token in the string
during parsing.
numbers[row][col] = st.nextToken();
System.out.println(row + " " + col + " = " + st.nextToken());
You go through your tokens without using them !