Trusted by Students Everywhere
Why Choose Us?
0% AI Guarantee

Human-written only.

24/7 Support

Anytime, anywhere.

Plagiarism Free

100% Original.

Expert Tutors

Masters & PhDs.

100% Confidential

Your privacy matters.

On-Time Delivery

Never miss a deadline.

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

Computer Science Dec 29, 2020

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.");
}
}
}

Expert Solution

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.");

        }

    }

}

Archived Solution
Unlocked Solution

You have full access to this solution. To save a copy with all formatting and attachments, use the button below.

Already a member? Sign In
Important Note: This solution is from our archive and has been purchased by others. Submitting it as-is may trigger plagiarism detection. Use it for reference only.

For ready-to-submit work, please order a fresh solution below.

Or get 100% fresh solution
Get Custom Quote
Secure Payment