Java- Set hasDigit to true if the 3-character passCode contains a digit.

  • Context: Java 
  • Thread starter Thread starter dellmac
  • Start date Start date
  • Tags Tags
    Java Set
Click For Summary
SUMMARY

The discussion focuses on determining if a 3-character passCode in Java contains a digit. The initial approach used the method Character.isDigit incorrectly by comparing it to the length of the passCode. A more effective solution involves iterating through each character of the passCode using a loop to check for digits. The final recommendation is to implement a loop that checks each character, ensuring the solution works for passCodes of varying lengths.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of character manipulation in Java
  • Familiarity with control structures, specifically loops
  • Knowledge of the Character class methods
NEXT STEPS
  • Implement a loop to check for digits in a string in Java
  • Explore Java's String class methods for string manipulation
  • Learn about regular expressions in Java for pattern matching
  • Investigate error handling for invalid passCode lengths
USEFUL FOR

Java developers, software engineers, and anyone interested in validating user input for security purposes.

dellmac
Messages
4
Reaction score
0
Been working this problem for hours and can't seem to get it right. Any assistance would be greatly appreciated!

Note: Green colored text is the editable text

public class CheckingPasscodes {
public static void main (String [] args) {
boolean hasDigit = false;
String passCode = "";
int valid = 0;

passCode = "abc";

if (passCode.equals(Character.isDigit(passCode.length()))) {
hasDigit = true;
}

if (hasDigit) {
System.out.println("Has a digit.");
}
else {
System.out.println("Has no digit.");
}

return;
}
}
 
Technology news on Phys.org
So I just got it. I believe I used the long way though; not sure if there is an easier way:

if (Character.isDigit(passCode.charAt(0))) {
hasDigit = true;
}
if (Character.isDigit(passCode.charAt(1))) {
hasDigit = true;
}
if (Character.isDigit(passCode.charAt(2))) {
hasDigit = true;
}
 
dellmac said:
So I just got it. I believe I used the long way though; not sure if there is an easier way:

if (Character.isDigit(passCode.charAt(0))) {
hasDigit = true;
}
if (Character.isDigit(passCode.charAt(1))) {
hasDigit = true;
}
if (Character.isDigit(passCode.charAt(2))) {
hasDigit = true;
}

Hi dellmac! (Smile)

That looks like the right direction.
But suppose the passCode is not length 3...
In other words, you need a loop:
Code:
for (int i = 0; i < passCode.length(); ++i) {
    if (Character.isDigit(passCode.charAt(i))) {
        hasDigit = true;
    }
}
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 2 ·
Replies
2
Views
7K
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
845
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K