Removing sentences containing a keyword in java
Tag : java , By : user165871
Date : March 29 2020, 07:55 AM
Hope this helps I am been searching online and on here on how I can remove a line that contains one or two words but I can't find anything on java. This is the code I have right now: , Something like this: //BufferedReader stuff etc.
List<String> words = new ArrayList<String>();
words.add("fun");
words.add("something");
String line;
while( (line = br.readLine()) != null)
{
boolean found = false;
for(String word: words)
{
if(line.contains(word))
{
found = true;
break;
}
}
if(found) continue;
System.out.println(line);
}
|
Finding Corresponding Letter Counting from Opposite End of Alphabet and Removing From String Recursively JAVA
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have modified your method a bit, have a look at it. You need compare with 155 if your string is all capitals, if all lower case letters you need compare with 219. As @Raghu suggested, this doesnt required recursion (that is making things complicated), but I am assuming you want to try this with recursion. public static boolean isAlphaOpp (String word)
{
//if word has odd number of characters, it cannot be an alpha opp
if (word.length() % 2 != 0)
{
return false;
}
//if string makes it to 0, then word must be an alpha opp
if (word.length() == 0)
{
return true;
}
/*if (word.charAt(0) + word.charAt(word.length()-1) == 155)
{
System.out.println(word.substring(1, word.length()-1));
return isAlphaOpp(word.substring(1, word.length()-1));
}
*/
//Should go thru each letter and compare the values with char(0). If char(0) + //char(i) == 155 (a match) then it should remove them and call the method again.
int length = word.length()-1;
int start = 0;
String newStr = null;
while(start < length) {
if(word.charAt(start) + word.charAt(length) == 219) {
StringBuilder sb = new StringBuilder(word);
sb.deleteCharAt(length);
sb.deleteCharAt(start);
newStr = sb.toString();
System.out.println(newStr);
start++;
length--;
break;
} else {
start++;
}
}
if(newStr != null) {
return isAlphaOpp(newStr);
}
return false;
}
|
How to replace one alphabet by same alphabet and new line character in Java regex
Tag : java , By : user122937
Date : March 29 2020, 07:55 AM
may help you . Hi I want to replace a string e.g. , You could try the below, (?<=>)(?=<)
String s = "<foo/><boo/><woo/>";
System.out.println(s.replaceAll("(?<=>)(?=<)", "\n"));
<foo/>
<boo/>
<woo/>
String s = "<foo/><boo/><woo/>";
System.out.println(s.replaceAll("(?!^)(<[A-Za-z])", "\n$1"));
|
JAVA removing a line from a .txt file with a keyword
Date : March 29 2020, 07:55 AM
it helps some times I've tested your code with minor changes (noted by @Andreas while I was writing this) and it works as expected public static void main(String[] args) {
try {
File inFile = new File("C:\\rirubio\\books.txt");
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File("C:\\rirubio\\books.txt" + "_temp");
BufferedReader br = new BufferedReader(new FileReader(inFile));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
String lineToRemove = JOptionPane.showInputDialog("Enter line to remove");
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().contains(lineToRemove)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile)) {
System.out.println("Could not rename file");
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
|
Removing keyword from list after matching the keyword with answer
Tag : chash , By : m0gb0y74
Date : March 29 2020, 07:55 AM
|