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

  • Thread starter Thread starter dellmac
  • Start date Start date
  • Tags Tags
    Java Set
AI Thread Summary
The discussion revolves around a coding problem related to checking for digits in a passcode string. The initial code attempts to determine if the passcode contains any digits but incorrectly uses `Character.isDigit` with the string length, leading to errors. A user later proposes a solution that checks each character individually, but acknowledges it may be inefficient. Another participant suggests a more efficient approach using a loop to iterate through the entire string, checking each character for digits. This method ensures that the code can handle passcodes of varying lengths effectively.
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;
    }
}
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top