Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Quizzma Latest Articles

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

In Java; Set hasDigit to true if the 3-character passCode contains a digit. If you could tell me why the code I wrote isn’t working I’d be thankful.

import java.util.Scanner;

public class CheckingPasscodes {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
boolean hasDigit;
String passCode;

hasDigit = false;
passCode = scnr.next();

let0 = userInput.charAt(0);
let1 = userInput.charAt(1);
let2 = userInput.charAt(2);

if ((Character.isDigit(let0) || Character.isDigit(let1) || Character.isDigit(let2)){
hasDigit = true;
}

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

Answer

import java.util.Scanner;

public class CheckingPasscodes {

public static void main (String [] args) {

Scanner scnr = new Scanner(System.in);

boolean hasDigit;

String passCode;

hasDigit = false;

passCode = scnr.next();

// the string is passCode and not userInput

// also you didn’t declare let0, let1, let2

int let0 = passCode.charAt(0);

int let1 = passCode.charAt(1);

int let2 = passCode.charAt(2);

if ((Character.isDigit(let0) || Character.isDigit(let1) || Character.isDigit(let2))){

hasDigit = true;

}

if (hasDigit) {

System.out.println(“Has a digit.”);

}

else {

System.out.println(“Has no digit.”);

}

}

}

import java.util.Scanner;  public class CheckingPasscodes {  public static void main (String [] args) {  Scanner scnr = new Scanner(System.in);  boolean hasDigit;  String passCode;  hasDigit = false;  passCode = scnr.next();  // the string is passCode and not userInput  // also you didn't declare let0, let1, let2  int let0 = passCode.charAt(0);  int let1 = passCode.charAt(1);  int let2 = passCode.charAt(2);  if ((Character.isDigit(let0) || Character.isDigit(let1) || Character.isDigit(let2))){  hasDigit = true;  }  if (hasDigit) {  System.out.println("Has a digit.");  }  else {  System.out.println("Has no digit.");  }  }  }

Was this helpful?




Quizzma Team

Quizzma Team

The Quizzma Team is a collective of experienced educators, subject matter experts, and content developers dedicated to providing accurate and high-quality educational resources. With a diverse range of expertise across various subjects, the team collaboratively reviews, creates, and publishes content to aid in learning and self-assessment.
Each piece of content undergoes a rigorous review process to ensure accuracy, relevance, and clarity. The Quizzma Team is committed to fostering a conducive learning environment for individuals and continually strives to provide reliable and valuable educational resources on a wide array of topics. Through collaborative effort and a shared passion for education, the Quizzma Team aims to contribute positively to the broader learning community.

Related Posts