Skip characters / numbers in JAVA
How to skip a number or character that is the same for two numbers or characters from each other while those characters / numbers exist in a string of numbers or word
example (not lists):
I want to skip the number 3 (in any position) when I am counting from 2 to 14
the result will be
2,4,5,6,7,8,9,10,11,12,14
the other will skip the number 31 in any combination where 3 and 1 come out if both exist
these two examples apply to symbols as well.
What I was doing was
for(int i = startingNum; i <= endingNum; i++){
if(i "has a" 3){
skip number;
}
else{
counter++;
}
}
combination of numbers
for(int i = startingNum; i <= endingNum; i++){
if((i "has a" 3) AND (i "has a " 1)){
skip number;
}
else{
counter++;
}
}
in the symbol I am completely lost ...
+3
source to share
2 answers
One approach is to use the parsing results, for example:
public static Integer isParsable(String text) {
try {
return Integer.parseInt(text);
} catch (NumberFormatException e) {
return null;
}
}
...
import java.io.IOException;
public class NumChecker {
static String[] str = new String[]{"2", "4", "a", "sd", "d5", "6", "7", "8", "a1", "3", "10", "11", "12", "14"};
static int startingNum = 2;
static int endingNum = 10;
static int counter = 0;
static int mark = 3;
public static Integer isParsable(String text) {
try {
return Integer.parseInt(text);
} catch (NumberFormatException e) {
return null;
}
}
public static void main(String[] args) throws IOException {
for (int i = startingNum; i <= endingNum; i++) {
Integer num = isParsable(str[i]);
if (num != null) {
if (num == mark) {
counter++;
}
}
}
System.out.println(counter);
}
}
OUTPUT:
1
+2
source to share