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 years ago.
Improve this question
OK lets say the secret pin is:
2236 (resident evil 2 reference)
if a person type 2200
I get a match count of 2;
But:
If the person type: 2000 or 0200, or 0020, or 0002
results says that it still matches 2 numbers.
It should only match one of the 2 number but I still get a match count of 2.
I'm currently using :
if(currentPin.indexOf(secretPin.charAt(i)) != -1)
on a loop.
UPDATE:
Assuming all the the necessary variables are declared and initialized and all scopes are closed
heres the code that does the matching
for (int i = 0; i < secretPin.length(); i++)
{
if (currentPin.indexOf(secretPin.charAt(i)) != -1)
{
System.out.println("Found");
match++;
}
}
//output:
2236 //SECRET NUMBER display
0002 //USER GUESS
Found
Found
MATCH: 2 // should only match 1 if USER GUESS only contain one number 2
Assuming the number of digits is always the same between secret pin and current pin, this simple algorithm would do the trick to retrieve the number of match :
public static void main(String[] args) {
String secretPin = "2236";
String currentPin = "6322";
int match = 0;
for (int i = 0; i < currentPin.length(); i++) {
if (secretPin.contains(String.valueOf(currentPin.charAt(i)))) {
secretPin = secretPin.replaceFirst(String.valueOf(currentPin.charAt(i)), "");
match++;
}
}
System.out.println(match);
}
"if(currentPin.indexOf(secretPin.charAt(i) != -1)" is getting a character, checks if it's != -1, and passes the boolean result to indexOf. You also have a bracket mismatch. As given this will not compile, let alone produce inaccurate output. Please paste the code you say you are able to run if you would like it reviewed.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Given that I have a string of number, like "12354789556", I need to check that string whether has digits from 0 to 9 at least once.
Can anyone tell me whether i can express this in regex please?
If your strings contains only digits for example "123548955664789556" then try:
System.out.println(myString.chars().distinct().count() == 10);
if your string can also contain letters for example sth like "bbb1235489556fhjerfs64789556"
System.out.println(myString.replaceAll("[^\\d]", "").chars().distinct().count() == 10);
With lookaheads :
^(?=.*0)(?=.*1)(?=.*2)(?=.*3)(?=.*4)(?=.*5)(?=.*6)(?=.*7)(?=.*8)(?=.*9)
If you want to restrict the string to digits only in addition to making sure it contains every digit :
^(?=.*0)(?=.*1)(?=.*2)(?=.*3)(?=.*4)(?=.*5)(?=.*6)(?=.*7)(?=.*8)(?=.*9)\d+$
Note that a version without lookaheads would be technically possible, but would realistically have to be crafted by code as it would have to enumerate all possible orders between digits (10! = 3628800 enumerations).
You can also do it in Java like this:
boolean containsAll = true;
for (int i = 0; i < 10; i++) {
if (!str.contains("" + i)) {
containsAll = false;
}
}
return containsAll;
A non-regex way would be to loop through the String and return false if the indexOf returns -1:
static boolean checkAll(String s, char[] allNums) {
for (int i = 0; i < allNums.length; i++) {
if (s.indexOf(allNums[i]) == -1) {
return false;
}
}
return true;
}
Example
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 years ago.
Improve this question
It's about string to make a compact output.
Example 1
Input : boooooob
Output : bob
Example2
Input : boobaabbiibbuuuuub
Output : bobabibub
Can anyone help me?
I'm stuck, thx.
This can be solved by using regular expression (\\w)\\1+
public class RemoveReplicateLetter {
public static void main(String[] args) {
//For input: boooooob
System.out.println(removeReplicateLetter("boooooob"));
//For input: boobaabbiibbuuuuub
System.out.println(removeReplicateLetter("boobaabbiibbuuuuub"));
}
public static String removeReplicateLetter(String word) {
/*
REGEX:
(\\w)\\1+
- \\w : matches any word character (letter, digit, or underscore)
- \\1+ : matches whatever was in the first set of parentheses, one or more times.
*/
return word.replaceAll("(\\w)\\1+", "$1");
//Here $1 means return letter with match in word by regex.
}
}
Output:
bob
bobabibub
This method should do the job:
public String simplify(String input) {
// Convert to an array for char based comparison
char[] inputArray = input.toCharArray();
// First char will always be included in the output because there is no char to compete
String output = String.valueOf(inputArray[0]);
// Check every char against the following
for (int i = 1; i < inputArray.length; i++) {
// If not equal
if (inputArray[i - 1] != inputArray[i]) {
// Add to output
output += inputArray[i];
}
}
// Return the result
return output;
}
It will compare every char with the following one and only adds it to the output if they are not equal.
Note: This is just a proof of concept, not an optimal solution.
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 years ago.
Improve this question
If I had a numberical idno value, how would I find the ones >=5000 and < 5000? Would i use idno.substring(7,11) and then check the conditions
if >=5000 print Male
if <5000 print Female
How would I perform this?
Check idno % 10000 to get the value of the last four digits.
Given that your ID number is stored as an integer:
int idno = ...; // some ID number
You can use this comparison:
if (idno % 10000 >= 5000) {
System.out.println("Male");
} else {
System.out.println("Female");
}
The if statement is a branching operator that allows your code to branch to one block or another based on the result of the comparison. This is different from a loop.
A loop is code that will execute repeatedly until a condition is met. For example, if you had an array of integer ID numbers:
int[] idArray = { .... , .... , .... }; // some array of IDs
You could loop through them like this:
for (int idno: idArray) {
if (idno % 10000 >= 5000) {
System.out.println(idno+" is Male");
} else {
System.out.println(idno+" is Female");
}
}
First convert your String to integer using
int idno1=Integer.parseInt(idno);
Then simple check idno1<=5000 or idno1>5000
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 8 years ago.
Improve this question
A beautiful number is a number containing only one type of digit, such as these: 0, 4, 44, 55555, 3333.
I would like to write an android app that finds out if a number is beautiful or not.
public static boolean isBeautiful(int n) {
char targetDigit = String.valueOf(n).toCharArray()[0];
for (char currentDigit : String.valueOf(n).toCharArray()) {
if (targetDigit != currentDigit) {
return false;
}
}
return true;
}
Bonus (sorry for the late addition of the bonus, but here it is), here's how to get all beautifuls up to x:
public static List<Integer> getBeautifuls(int upTo) {
List<Integer> beautifuls = new ArrayList<>();
int next = 0;
while (upTo >= next) {
beautifuls.add(next);
if (String.valueOf(next).toCharArray()[0] == '9') {
next = Integer.valueOf(new String(new char[String.valueOf(next).length() + 1]).replace("\0", "1"));
} else {
next = next + Integer.valueOf(new String(new char[String.valueOf(next).length()]).replace("\0", "1"));
}
}
return beautifuls;
}
Repeted string algorithm based on user102008's excellent answer on this question: Simple way to repeat a String in java
Note: Using Strings to create ints is a bit hack-ish and not so fast.
Try to check how many digits the number has, for example the number has 5 digits(between 10000-99999 and >= 11111)
if(num % 11111 == 0){
//it is beautiful number
}else{
//it is not beautiful
}
This is only an example for 5 digit numbers. You can develop an algorithm for checking other num of digits. I don't know is there any algorithm created before.
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 9 years ago.
Improve this question
I am trying to convert the tenth digit of an array to a number (10) if it is the value 'x'.
I have tried...
if (array[9] === 'x') {
'x' === 10;
};
Thanks
Try this, assuming that array is a char[]:
if (array[9] == 'x') {
array[9] = 10;
}
By the way, the code you posted is not valid for Java. This is not an operator: ===, we must use = for assignment and the trailing ; after the last } is unnecessary.
I edited your code already.
if (array[9] == 'x') //Line 1
{
array[9] = 10; // Line 2
}
On Line 1 you said ===. Java uses == to check if two primitive values are equal in value.
On Line 2 you used three equal signs again. If you want to reassign a variable, you will need to use ONE equal sign.