-
February 16th, 2018, 10:25 AM
#1
Search for a repeated subtsring and print out all the string value only once in java
Hi
I am trying to read the a string that shows the version name from a text file and print it. I only want to print the string once as it occurs a few times in the text log file. I managed it with the code below that should do it but keep it compilation errors. the string I am interested in the file is as follow: Version software 219 build. The error I am getting is: The constructor FileReader(String) is undefined
Code:
Map<String,Integer> map=new HashMap<String, Integer>();
FileReader freader = new FileReader("C:\\abc.log");
BufferedReader br = new BufferedReader(freader);
String s, t = "";
while((s = br.readLine()) != null) {
t = t + s;
}
freader.close();
String b[] = t.split("\n");
int j = 0;
while(j < b.length) {
String c[] = b[j].split(" ");
for(int i = 0; i < c.length; i++) {
if(c[i].equals("Version")) {
String r = "";
for(int k = i; k < i+4; k++)
r = r + c[k] + " ";
map.put(r, j);
}
}
j++;
}
Map<String, Integer> mapResult;
for (Entry<String, Integer> entry : mapResult.entrySet()) {
String distance = entry.getKey();
Integer value = entry.getValue();
System.out.println(entry.getKey());
}
}
Can anyone check how to fix it and if it work on their set up?
-
February 16th, 2018, 05:02 PM
#2
Re: Search for a repeated subtsring and print out all the string value only once in j
Originally Posted by hm99
The error I am getting is: The constructor FileReader(String) is undefined
Are you sure you have imported everything you should? Add these,
import java.io.File;
import java.io.FileReader;
Are you sure you haven't defined your own FileReader class. Try this to make sure you're using the right FileReader,
java.io.FileReader freader = new java.io.FileReader("C:\\abc.log");
Last edited by wolle; February 17th, 2018 at 03:16 AM.
-
February 16th, 2018, 07:12 PM
#3
Re: Search for a repeated subtsring and print out all the string value only once in j
The readLine method stops reading data when it finds a lineend and returns the data without the lineend character. The split won't find the \n
Concatenating s and t without an intervening character would concatenate the end of one line with the start of the next line.
Note: single letter variable names makes the code hard to read and understand. variable names should describe the data they hold.
Norm
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|