how to count duplicate Name in java [closed] - java

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

Related

Deleting lines from String containing a certain character [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
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.

What can be the algorithm to find the max number that appear the most in an array? [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
I'm trying to find out an algorithm that returns the number that has more occurrences in an array that contains number from 0 to 9 and that has a complexity of n.
I though to using an HashMap but it would require n^2
If anyone can write the code down,i'd prefer in Java but pseudocode is the same
Use ten counters (one per digit), scan the array and increment the counters corresponding to the digits. (You are actually computing the histogram of the digit frequencies.)
Report the digit with the largest counter.

How to alter Java strings contain only certain alphabetical characters [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 6 years ago.
Improve this question
how can I alter text string fields in java to only contain certain alphabetical characters (f,-)
in this format: 2f5-4, 2f6, 8f9
Only numbers f numbers and
numbers f number - numbers
You could use a regular expression to check, if the String in your text field is valid:
\d+f\d+(?:-\d+)?
Java code sample
Pattern pattern = Pattern.compile("\\d+f\\d+(?:-\\d+)?");
for (String s : new String[] {
"2f6", "2f9", "6f10", "5f9-2", "3f4-9"
}) {
System.out.println("String: \""+s+"\" match: "+pattern.matcher(s).matches());
}
Regex expression =>
(([0-9]+)([f])([0-9]*)|(([-])([0-9]*)))\w+

create an array from elements in a string, elemets start with certain key/sign [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 6 years ago.
Improve this question
I want to abstract an array with text strings that reside in a text, each text element starts with a certain sign or key e.g. $ or &. how can I achieve this?
so "$Huey, $Dewey, and $Louie all live in Duckcity. $Goofy and $Mickey live there too." should result in
string characters = {"Heuy","Dewey", "Louie", "Goofy","Mickey"};
Use Streams and a filter.
String s = "$Huey, $Dewey, and $Louie all live in Duckcity. $Goofy and $Mickey live there too.";
String[] a =
// Split it into words.
Arrays.stream(s.split("[, .]"))
// Pick only the words starting with '$'
.filter(w -> w.startsWith("$"))
// Remove the '$'
.map(w -> w.substring(1))
// Make a list.
.collect(Collectors.toList())
// And turn it into an array.
.toArray(new String[0]);
System.out.println(Arrays.toString(a));
Prints:
[Huey, Dewey, Louie, Goofy, Mickey]
Go through the elements and check whether or not the string starts with the sign/key. If it does, add it to a String[]

Reading fixed width text file with different length [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 6 years ago.
Improve this question
I have a text file that is in fixed width format, except each column has a different length. I have about 100 columns in all.
For example, the first few columns of text have the following width:
30
4
188
Let's assume I read in the first row from the file. How do I seperate/map the text into 100 different size columns?
We just dealt with this at work within the last few weeks. The way we went about solving the problem was to create an enum class with corresponding "indexes" to represent the start and end positions of the fields that needed to be extracted. This enum is loaded into a map of FIELD_NAME --> RANGE (i.e. 0:8) upon instantiation of the class that parses the message.
High-level, upon receipt of a message on the queue:
convert TextMessage to string
read line
for each field, get the corresponding range from the map
split the range on ":" to get the indexes
extract the values from the String using substring(index1,index2)
perform transformations (string to date, string to numbers, etc)
persist to database

Categories

Resources