Get values till until a character(include the checking character) using stringbuilder - java

How to check if a begining of line is "A" followed with anything is Ablahblablah
StringBuilder sb = new StringBuilder();
String[] lines1 = sb.toString().split("\\n");
for(String getVal: lines1){
while(getVal.contains("^A\\w*")){
temp.append(getVal);//If found store all the former values and line including A + 2 characters in a temp buffer
}
}
System.out.println("temp = " + temp.toString());
output
temp =
Temp returns empty.
Am I doing anything wrong.
UPDATE
used : if(getVal.startsWith("6000")){
I am getting the desired output.
Any issues in this logic.
how to delete the temp from existing sb?

You need to use matches() method, not contains().

Why not just do
if (getVal.startWith("A")) {
...

like this:
StringBuilder sb = new StringBuilder();
.
.
.
String[] lines1 = sb.toString().split("\\n");
for(String getVal: lines1){
while(getVal.matches("A\\w*")){
temp.append(getVal);//If found store all the former values and line including A + 2 characters in a temp buffer
}
}
System.out.println("temp = " + temp.toString());

Thanks every one
I found the answer
String[] lines1 = sb.toString().split("\\n");
StringBuilder temp = new StringBuilder();
for(String getVal: lines1){
if(getVal.startsWith("6000")){
temp.append(getVal);
break;
}
else{
temp.append(getVal);
}
temp.append("\n");
}

Its because of scope of temp.have you declared variable temp outside of loop?

Related

how to find i of a token in an array[i]

So, I've found a word in a document and print the line in which the word is present like this:
say example file contains : "The quick brown fox jumps over the lazy dog.Jackdaws love my big sphinx of quartz."
FileInputStream fstream = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while((strLine = br.readLine()) != null){
//check to see whether testWord occurs at least once in the line of text
check = strLine.toLowerCase().contains(testWord.toLowerCase());
if(check){
//get the line, and parse its words into a String array
String[] lineWords = strLine.split("\\s+");
for(int i=0;i<lineWords.length;i++){
System.out.print(lineWords[i]+ ' ');
}
And if I search for 'fox' , then linewords[] will contain tokens from the first sentence. and linewords[3] = fox. To print the color of the fox, I need linewords[2].
I was wondering how can we get the 'i' of a token in that linewords[i], because I want the output to be linewords[i-1]
You could use a hashMap which stores the word and a list with the indices.
HashMap<String, List<Integer>> indices = new HashMap<>();
So in the for loop you fill the HashMap:
for(int i=0;i<lineWords.length;i++){
String word = lineWords[i];
if (!indices.contains(word)) {
indices.put(word, new ArrayList<>();
}
indices.get(word).add(i);
}
To get all the indices of a specific word call:
List<Integer> indicesForWord = indices.get("fox");
And to get the i - 1 word call:
for (int i = 0; i < indicesForWord.size(); i++) {
int index = indicesForWord[i] - 1;
if (index >= 0 || index >= lineWords.length) {
System.out.println(lineWords[index]);
}
}
If you are using Java 8, it is straightforward:
List<String> words = Files.lines(Paths.get("files/input.txt"))
.flatMap(line -> Arrays.stream(line.split("\\s+")))
.collect(Collectors.toList());
int index = words.indexOf("fox");
System.out.println(index);
if(index>0)
System.out.println(words.get(index-1));
This solution works also when the word you are searching is the first words in a line. I hope it helps!
If you need to find all occurences, you can use the indexOfAll method from this post.
That can be done by traversing the array and when you get your word , print the one before it.Here's how:-
if(lineWords[0].equals(testWord)
return;//no preceding word
for(int i=1;i<lineWords.length;i++){
if(lineWords[i].equals(testWord){
System.out.println(lineWords[i-1]);
break;
}
}

How to replace spaces in Java in a efficient and better way

I referred to this link
http://www.crazyforcode.com/replace-spaces-string-%20/
and couldn't find any similar implementation for Java in Stackoverflow
I have implemented a similar logic to replace spaces to %20 using the below code.
String sen="I need to replace all the spaces to %20";
String[] arr = sen.split(" ");
StringBuffer buff = new StringBuffer();
for (String str : arr) {
buff.append(str);
buff.append("%20");
}
But there are many issues with the logic, like it adds %20 to the last of every sentence. Is there any efficient way of doing this ??
The best and the most efficient way of doing this is to use a inbuilt library to implement this for you. You can use
java.net.URLEncoder
and you can find the API here http://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html#encode-java.lang.String-java.lang.String- or here https://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html
if you don't want to use predefined methods, do the following:
String str = "t e s t";
str = str.replace(" ", "%20");
System.out.println(str);
This will print out "t%20e%20s%20t".
You can try to substring the output of the StringBuffer.
String sen = "I need to replace all the spaces to %20";
String[] arr = sen.split(" ");
StringBuffer buffer = new StringBuffer();
for (String str : arr) {
buffer.append(str);
buffer.append("%20");
}
String bufferString = buffer.toString();
String stringWithoutSpaces = bufferString.substring(0, bufferString.length() - 3);
but another more simple solution would be:
sen = sen.trim().replaceAll(" ", "%20");
Don't know Java, but if its like c++ a bit, use the fastest method.
It might be old fashioned, but still the fastest.
pseudocode
buff = "";
if ( arr.size() > 0 ) {
buff = arr[0];
for (int i=1; i<arr.size(); i++)
buff.append("%20" + arr[i] );
}

Issue removing punctuation and capital letters

I am trying to read a text file and create a hash map with unique words and their frequency. I searched for a method of removing punctuation and tried implementing it, but it doesn't seem to be working.
I tried using the following in the fourth line of code: line = line.replaceAll("\p{Punct}+", "");
Am I missing something?
try (BufferedReader br = new BufferedReader(new FileReader("Book 1 A_Tale_of_Two_Cities_T.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
line = line.replaceAll("\\p{Punct}+", "");
while (line != null) {
String[] words = line.split(" ");//those are your word
for (int i = 0; i < words.length; i++) {
if (m1.get(words[i]) == null) {
m1.put(words[i], 1);
} else {
int newValue = Integer.valueOf(String.valueOf(m1.get(words[i])));
newValue++;
m1.put(words[i], newValue);
}
}
sb.append(System.lineSeparator());
line = br.readLine();
}
}
Map<String, String> sorted = new TreeMap<>(m1);
for (Object key : sorted.keySet()) {
System.out.println("Word: " + key + "\tCounts: " + m1.get(key));
}
The output I am expecting looks like this:
Word: there Counts: 279
Word: thereupon Counts: 1
Word: these Counts: 156
The issue is that I am also getting this as output:
Word: these, Counts: 3
Word: these. Counts: 2
Word: these.’ Counts: 1
I would like the punctuation removed from the end (and beginning) of the words and have them added to the count of "these", etc.
Thanks for your help!
You are running your replaceAll after reading the first line:
String line = br.readLine();
line = line.replaceAll("\\p{Punct}+", "");
So the first line will not have any punctuation. But then, you go into this while loop:
while (line != null) {
...
line = br.readLine();
}
So there is no replaceAll inside the loop. At its end you read another line. And then you loop back to the while. Since there is no replacement inside the loop, the second line and those that follow it will retain the punctuation.
The replacement should be done inside the loop. Moreover, it shouldn't be done right after you read the first line, because that very first line might be null in theory (if the file is empty).
So what you should do is do it inside the loop after you verify that the line is not null:
String line = br.readLine();
while (line != null) {
line = line.replaceAll("\\p{Punct}+", "");
...
line = br.readLine();
}
Now, it tests if the line is null, and then replaces the punctuation in it. And since the replacement is done inside the while, it will also be applied to the second line and those that follow it.
As RealSkeptic pointed out, you need to put the regex replace inside the loop.
There are several other "problems" with your code, but the main problem is there's just so much of it.
Here's how you can do it in one (albeit long) line:
Files.lines(Paths.get("Book 1 A_Tale_of_Two_Cities_T.txt")
.map(s -> s.replaceAll("\\p{Punct}", "").toLowerCase()))
.flatMap(s -> Arrays.stream(s.split("\\s+")))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())
.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach((k, v) -> System.out.println("Word: " + v + "\tCounts: " + v));
Disclaimer: Code may not compile or work as it was thumbed in on my phone (but there's a reasonable chance it will work)

Java - take name from string

I'm developing a Java application that make some statistic stuff.
This application take all data from a .txt file which is supplied by the user.
The first line of that file contains the name of the sets of data that follows like this:
velx,vely,velz
//various data
I need to analyze that first line and retrieve the three name of variables, I correctly get the first two but I'm not able to get the last one.
There the code to get names:
public ArrayList<String> getTitle(){
// the ArrayList originally is not here but in the class intestation
// I copied it here to simplify code's understanding
ArrayList<String> title = new ArrayList<String>();
try {
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
int titleN = 0;
String line = br.readLine(); //read the first line of file
String temp;
System.out.println(ManageTable.class.getName() + " Line: " + line);
int c = line.length();
for(int i = 0; i <c; i++){
if((line.charAt(i) == ',') || **ANOTHER CONDITION** ){
temp = sb.toString();
System.out.println(ManageTable.class.getName() +" Temp is: " + temp);
title.add(temp);
System.out.println(ManageTable.class.getName() + " Title added");
sb.delete(0, sb.length());
}else{
sb.append(line.charAt(i));
}
}
} catch (IOException ex) {
Logger.getLogger(ManageTable.class.getName()).log(Level.SEVERE, null, ex);
}
return title;
}
I need to add a second condition to the if statement in order to find out when the line is ended and save the last name, even if its not followed by ','
I tried using:
if((line.charAt(i) == ',') || (i==c))
but from the name I get, always miss a character.
How can I check the end of the line and so get the full name?
If line contains just three names separated by comma, you can do
String[] names = line.split(",");
No need for all this looping. You can just split the line around the comma to get an array:
String[] names = line.split(",");

sending all read lines to string array

I have a concept but I'm not sure how to go at it. I would like to parse a website and use regex to find certain parts. Then store these parts into a string. After I would like to do the same, but find differences between before and after.
The plan:
parse/regex add lines found to the array before.
refresh the website/parse/regex add lines found to the array after.
compare all strings before with all of string after. println any new ones.
send all after strings to before strings.
Then repeat from 2. forever.
Basically its just checking a website for updated code and telling me what's updated.
Firstly, is this doable?
Here's my code for part 1.
String before[] = {};
int i = 0;
while ((line = br.readLine()) != null) {
Matcher m = p.matcher(line);
if (m.find()) {
before[i]=line;
System.out.println(before[i]);
i++;
}
}
It doesn't work and I am not sure why.
You could do something like this, assuming you're reading from a file:
Scanner s = new Scanner(new File("oldLinesFilePath"));
List<String> oldLines = new ArrayList<String>();
List<String> newLines = new ArrayList<String>();
while (s.hasNext()){
oldLines.add(s.nextLine());
}
s = new Scanner(new File("newLinesFilePath"));
while (s.hasNext()){
newLines.add(s.nextLine());
}
s.close();
for(int i = 0; i < newLines.size(); i++) {
if(!oldLines.contains(newLines.get(i)) {
System.out.println(newLines.get(i));
}
}

Categories

Resources