Deleting lines from String containing a certain character [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
any help with this would be greatly appreciated.
code in Java SE 8.
A string is inputted to represent a table.
if a row in the table contains the word NA it must be removed. ex..
if given String = "id,name,age,score\n1,Tom,NA,20\n17,Susan,25,15"
the table would be displayed as follows;
id name age score
1 Tom NA 20
5 Susan 25 15
As the second row contains 'NA' it needs to be removed. Ps Case matters, so if it says 'na' it doesn't need removed.
I have tried the following:
input = input.replaceAll("NULL,", "");
String lines[] = input.split("\\r?\\n");
for (String line : lines) {
System.out.println(line);
The problem is I need to remove the entire row where there is an occurrence of the word 'NA'
any ideas?

I recommend splitting the String with String.split('\n'); That will return an Array you can work with much easier. With that you can just filter the array for Strings that include NA.

Related

Find all the occurences of an array that contain a substring in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Input:
[the, and, land, wander, dreams]
Substring: "and"
Output: 3
I need to find all the occurrences of an array that contain a certain substring but all I found was the word itself. For example, I want it to count the "and" in the word "land" and "wander" as well. I don't know how to do that. Please help!
EDIT: Updated the code.
What about:
int cnt=0;
String[] input = {"the", "and", "land", "wander", "dreams"};
for (String str : input){
if (str.contains("and")){
cnt++;
}
}
System.out.println(cnt);
This code should work for you. But keep in mind following - Exbow is right, next time your question might be considered as useless and will be closed.

How to print a read new line character from a file in java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
File contents:
Suspect 2:\n
Name: Henry\n
Profession: Store Clerk\n
Relation to victim: Were best friends\n
Reason for being a suspect: He was at victims location for 30 minutes\n
Look: Brown, 6 foot, Hispanic\n
Hobbies: Reading\n
History: Aggravated Assault Charge\n\n\
I am wanting to use a loop to read in line for line and then merge all the read lines and finnaly be able to print the string. However, if I do so it prints the \n instead of actually printing a new line. Does anyone have any idea on how to make it print new lines instead of \n?
I would suggest removing the \ns and just using System.out.println() as soon as each line is read. Or if you need to keep the strings in memory for later, you could store the strings in a string array. Then loop through the array and print it out using System.out.println().

Regex to get the last value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
From the following value
[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]
I want to get the last number 2196879032
Some times value will be -1
[AAMP-PLAYER]aamp pos: [14..187..202..-1]
Instead of split method how can i extract last digit using regex method
You could match the last (possibly negative) number at the end of the string, just before the closing ]:
(\-?\d+)\]$
A number is last if it is not followed (following it anywhere, not just immediately) by any other number.
(\-?\d+)(?!.*\d)
We could try using a String#replaceAll option here:
String input = "[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]";
String num = input.replaceAll("^.*\\.\\.(-?\\d+).*$", "$1");
System.out.println(num);
This prints:
2196879032

Remove Known Part of String Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
A little help please. I want to strip-off a portion of my string which is made up of a number in Java.
eg. Have the following Strings - 60067 ; 600567 and 600876 and '600676600'
How do I remove 600 from each of the strings in Java so I remain with
67 ; 567 and 876 and '676600'
As Pavneet said in his comment, this works well:
"60067".substring(3);
Also, if you want to make sure it is the string "600" that you are removing:
"60067".replaceFirst("600", "");
Java Strings have no remove method, so it's common to replace the String with a blank one, "".
try this:
String s = "60067 ; 600567 and 600876";
s = s.replaceAll("600", "");
System.out.println(s);
I got
67 ; 567 and 876

how to count duplicate Name in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Suppose you have a string as input which has names separated by the underscore character. Write a program that outputs the same string after removing any duplicate names in it (retain first occurrence when multiple occurrences are present), and adds the number of times the name occurs, immediately after the name. For example, Tom_Dick_Harry_Joe_Dick_Chris becomes Tom1_Dick2_Harry1_Joe1_Chris1.
I will provide no code but the following might help :
Split the array with '_'
Maintain a Map<String,Integer> occurrences for counting the occurrences of each name and a List<String> apparitionOrder
For each word of the array :
if the word is in occurrences, increment the value associated to the key
else, put this name in the map associated with 0 and append it to apparitionOrder
Instantiate a new array of the size of apparitionOrder
Fill the new array accordingly to apparitionOrder and occurrences

Categories

Resources