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 7 months ago.
Improve this question
I will use examples to explain this. I have two fields. Summary and Labels. An example of Labels would be Labels = " QWE,CD-YTO, YTOU" and Summary = "CD-YTO : Some random words".
Now my code first checks labels for any element that starts with "CD-" and if its present it, that's pretty much it. It takes that value, for example here it takes "CD-YTO". Now, in case labels does not have any element that starts with "CD-" then its supposed to check summary. That's where my problem lies.
The code i developed converts summary to an String[] by using .split(" ") and the just check for .contains("CD-"). but the code complexity increased and i was told to reduce it.
Summary can be like the following
"CD-YTO: Some random words"
"CD-RTY:Some random words"
"CD-RTUU Some random words"
"cd-GFY - Some random words"
Now, in all the above cases, im supposed to get the "CD-***" part only. Using if loop for all these cases just increases the code complexity. Is there any way i can reduce that?
If your only concern is for loop, you can try to do the following:
String[] result = Arrays.stream(yourString.split(" "))
.filter(str -> str.contains("CD-"))
.toArray();
Related
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 1 year ago.
Improve this question
I'm trying to learn Java. My current assignment is to build a simple four function calculator..... this would be easy given if/else and/ or switch statements, but I'm supposed to build this using methods.
The original input has to be put in as a single string, so, in my mind, I'm going to have to take the single string and create substrings, then somehow convert these substrings into double values, while deleting whatever whitespace could possibly be between characters. My current idea is to somehow identify the "+,-,*, or /" within the string and divide into substrings before and after these values, using the appropriate defined method for whichever operator to do the calculations....
The problem is that I can't see a good way to divide these up into substrings or how to convert the numbers involved into double values. Anyone got any advice for me? Keep in mind, what we have gone through is pretty limited and I feel like I'm missing something REALLY simple out there.
You can split a string based on a particular character using str.split("\\+"), for example. You can convert the split pieces of the string to doubles by using Double.parseDouble(str);
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
So I'm working on a school project and it includes a Combined Gas Law calculator where the user may input the temperature in °celsius (e.g. 1°C) and the code converts it to kelvin; if the user's input is in kelvin already, it does not do that and continues with the equation. So does anyone know how I can separate the two data types into two different variables in Java?
You could split over a specific string (degree character for example), store in a String array and parse the first element. Something like this:
String str = "47°C"
String[] strArray = str.split("°");
int number = Integer.parseInt(strArray[0]);
Congratulations, you are working on something but not writing it's code. I can tell you a few tips about how to implement it's code.
You have a string that has some numbers in it, and also has unit. Try searching 'How to extract numbers from a string'. Now you have a number.
Find the unit in the string by looking the last character in the string.
If the condition is ok for your homework, calculate the new value.
Print the result.
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 4 years ago.
Improve this question
I'm making a textbook indexer and I'm formatting the output file. there are periods between the word and its frequency.
desired textfile output:
word..............25:
word...............3:
word...............2:
I'm trying to get the lines to be aligned to the : but I can't get it under the semicolon if the numbers are more than 1 digit.
Does anybody have any ideas for java string formatting for the desired output?
for reference the biggest word is 23 characters and it's frequency is 1.
Since in the comment, the OP attempted to use String.format(), here is an approach to consider. Rather than trying to get the number to align right with the "%23d", align the word and the count separately.
String.format("%-23s%2d:", getWord(), count);
The %-23d will format the getWord() in 23 spaces, left aligned, then the %2d will right align the "count".
Example output:
Hello...................9:
Goodbye................42:
whatever...............17:
Note I just used the same .replace() approach as the OP for quickness sake.
See this attempt here at ideone.com
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 5 years ago.
Improve this question
Write a java code to check if the given string is even or not? Eg. aabbcc, aacbbc is even string.
I was asked this program in one interview. Actually i did not understand what is frequency here.
For a string s of length n, consider s[0] XOR s[1] ... XOR s[n - 1] where [i] is the (i)th letter of the string. Use java.lang.String#charAt(int) in java to extract a character.
If that is zero you have an even string, else you have an odd string.
Test n % 2 first for an immediate pay rise: If that is not zero then there must be at least 1 occurrence of a character that appears an odd number of times.
Normally folk who wrote computer games in machine code as kids in the 1980s will ask this question as it seems obvious to them. I doubt it is any more: XOR was a very fast way of writing sprite images.
Depending on what the interviewer was asking, string frequency is either,
how many times a string is found in another string.
how many times a character is found in a string.
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 7 years ago.
Improve this question
consider the following Strings:
he llo
goodbye
hello
= (goodbye)
(he)(llo)
good bye
helium
I'm trying to sort these in such a way that similar words comes together, I know
alphanumerical sorting is not an option
removing special chars ",-_ and etc then comparing is certainly helpful but results won't be as good as I hope for.
NOTE :
there might be few different desired ouput for this, one of which is :
DESIRED OUTPUT:
hello
he llo
(he)(llo)
helium
goodbye
good bye
= (goodbye)
so my question is that if there is a java package that compares strings and ultimately sort them based on it .
I've heard of terms such as n-gram and skip-gram but didn't quite understand them. I'm not even sure if they can be useful for me at all.
UPDATE:
finding similarities is certainly part of my question but the main problem is the sorting part.
Here's one possible approach.
Calculate the edit distance/Levenshtein distance between each pair of strings and then you use view the strings as a complete graph where the edge weights come from the edit distance. Choose a threshold for those weights and remove all the weights that to high. Then find the cliques in this graph. If your threshold is fairly low perhaps even finding connected components would be an option.
Note:
Perhaps it would be better to substitute some edit distance with one of the similarity measures in the link that #dognose posted.
Also, note that finding cliques will be very slow if you have a large numbers of strings