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
How can I remove all characters being neither '0' nor '1' in a string, so that the final string consists of only 0s and 1s? I'm using Java 8.
My solution with a for loop (I think it's not the most efficient way):
String before = "012940124810";
String after = new String();
for(int i = 0; i < before.length(); i++) {
if(before.charAt(i) == '0' || before.charAt(i) == '1') {
after += before.charAt(i);
}
}
you can use regex and replaceAll method
String out = str.replaceAll("[^01]", "");
To make this you just take a index variable starting from 0 and delete character by charcter if it is not 0 or 1 if you found just move index to next location without deleting it. If you need more help attach some code so that i will tell you perfect solution
Related
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
I'm studying implementation with Kotlin. When I implement Kotlin version by below situation, I can't imagine cool way.
for(i = 0 ; i + 8 <= table.size; i++){
for(j = 0 ; j + 8 <= table[0].size; j++{
whatever
}
}
Above code is Java version
for(i in 0 until table.size){
if(i+8 > table.size)break
for(j in until table[0].size){
if(j+8 > table[0].size)break
whatever
}
}
Above is Kotlin version which I think.
Is this fine way?
You can just move the -8 into the upper limit, and since you include (<=) the upper limit you shouldn't be using until, but the regular range expansion with two dots.
So it becomes:
for (i in 0..table.size-8){
for (j in 0..table[i].size-8){}
}
(I imagine you would also want to replace the magical number eight with a variable with a meaningful name)
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
How to get the difference of letter in two string in Java?
String s1 = "2 + 5 = 7";
String s2 = "2 5 7";
String difference = StringUtils.difference(s1, s2);
System.out.println(difference);
I want the output +1|=2 As + is next after first character and = is next after second character while checking the difference between the two string.
How can I implement this program in java?
just check every character from the string in a loop, and if ch1 != ch2 you output the difference.
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 2 years ago.
Improve this question
I need to write a for loop in java that displays all of the numbers ranging from 13 - 93 that end in the number 3. It must include 13 and 93.
Loop from 13 to 93. Increment by 10. Like,
for (int i = 13; i <= 93; i += 10) {
// Print the value
}
If you wanted, you could write a function that achieves this task but with parameters
that allow for more flexible control. Where it takes in arguments representing a
from value and a to value, and something like a endsWith value. It's just a suggestion though.
Any decimal based number mod 10 will get you the last digit.
for (int i = start ; i <= end ; i++) {
if (i % 10 == 3) {
System.out.println(i);
}
}
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
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 6 years ago.
Improve this question
I need to know the logic behind this java question:
Identify the character shifts to for the following entered text to
become "aptech" (only Aptech will be entered with character shifts).
Input: "bqufdi"
Output: "Character Shift = 1"
Does anyone help me out on this java question, please? Thanks.
String s = "aptech";
String in = "bqufdi";
You could write a simple for statement like
for(int i = 0; i < in.length(); ++i)
System.out.print(in.charAt(i) - s.charAt(i)); // shift for each letter
That outputs 111111 and means that all letters in "bqufdi" are shifted to one position on the right.