Question

In: Computer Science

(JAVA)Set hasDigit to true if the 3-character passCode contains a digit.

JAVE

3.10.1: String with digit.

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

MUST USE THIS TEMPLATE:

public class CheckingPasscodes {
public static void main (String [] args) {
boolean hasDigit;
String passCode;

hasDigit = false;
passCode = "abc";

/* Your solution goes here */

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

Solutions

Expert Solution

//The added solution is in bold form.
 
public class CheckingPasscodes {
 
public static void main (String [] args) {
 
boolean hasDigit;
 
String passCode;
 
hasDigit = false;
 
passCode = "abc";
 
//Begin the for loop.
 
for(int i = 0 ; i < passCode.length(); i++)
 
{
 
     //Extract a character from passCode
 
     //and store it in the variable ch.
 
     char ch = passCode.charAt(i);
 
     
 
     //Check the extracted character
 
     //to be digit or not.
 
     if(Character.isDigit(ch))
 
     {
 
          //Update the variable hasDigit to true.
 
          hasDigit = true;
 
     }
 
}
 
if (hasDigit) {
 
System.out.println("Has a digit.");
 
}
 
else {
 
System.out.println("Has no digit.");
 
}
 
}
 
}

Expert Solution

// CheckPasscodes.java

import java.util.Scanner;

public class CheckPasscodes {

   public static void main(String[] args) {

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner scnr = new Scanner(System.in);
       boolean hasDigit;
       String passCode;
      
       hasDigit=false;
       passCode=scnr.next();
      
       for(int i=0;i<passCode.length();i++)
       {
           if(Character.isDigit(passCode.charAt(i)))
           {
               hasDigit=true;
               break;
           }
       }
       if(hasDigit)
       {
           System.out.println("Has digit.");
       }
       else
       {
           System.out.println("Has no digit.");
       }
      
   }

}
________________________

Output#1:

Hello5Kane
Has digit.

________________________

Output#2:

HelloKane
Has no digit.




Related Solutions

(In C)Set hasDigit to true if the 3-character passCode contains a digit.
  Set hasDigit to true if the 3-character passCode contains a digit. (in C) #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h>   int main(void) { bool hasDigit; char passCode[50]; hasDigit = false; scanf("%s", passCode); /* Your solution goes here */ if (hasDigit) { printf("Has a digit.\n"); } else { printf("Has no digit.\n"); } return 0; }
(C++)Set hasDigit to true if the 3-character passCode contains a digit.
C++. Set hasDigit to true if the 3-character passCode contains a digit. #include #include #include using namespace std; int main() {bool hasDigit;string passCode; hasDigit = false;cin >> passCode; /* Your solution goes here */ if (hasDigit) {cout
Set hasDigit to true if the 3-character passCode contains a digit. public class CheckingPasscodes { public...
Set hasDigit to true if the 3-character passCode contains a digit. public class CheckingPasscodes { public static void main (String [] args) { boolean hasDigit = false; String passCode = ""; int valid = 0; passCode = "abc"; /* Your solution goes here */ if (hasDigit) { System.out.println("Has a digit."); } else { System.out.println("Has no digit."); } return; } }
(In JAVA)Write code to print the location of any alphabetic character in the 2-character string passCode.
Java Language Write code to print the location of any alphabetic character in the 2-character string passCode. Each alphabetic character detected should print a separate statement followed by a newline. Ex: If passCode is "9a", output is: Alphabetic at 1 import java.util.Scanner;   public class FindAlpha {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String passCode;              passCode = scnr.next();     ...
In Java, write a program that finds the first character to occur 3 times in a...
In Java, write a program that finds the first character to occur 3 times in a given string? EX. Find the character that repeats 3 times in "COOOMMPUTERRRR"
Please write a Java method contains that checks whether the second given character array is contained...
Please write a Java method contains that checks whether the second given character array is contained in the first given character array. We require that both of the arrays are partially filled.
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character is a vowel, and otherwise returns false. Also write a program to test your method. 2) Write a program that prompts the user to input a sequence of characters and outputs the number of vowels. (Use the method isVowel written in Programming Exercise 1.)
Java Code!!!! A five-digit number is said to be friendly if: the leftmost digit is divisible...
Java Code!!!! A five-digit number is said to be friendly if: the leftmost digit is divisible by 1 and the leftmost two digits are divisible by 2 and the leftmost 3 digits are divisible by 3 and the leftmost 4 digits are divisible by 4 and leftmost 5 digits (the five-digit number itself) is divisible by 5. For example, the number 42325 is a friendly number: 4 is divisible by 1 and 42 is divisible by 2 and 423 is...
a. How many 3-digit numbers can you make using the digits from the set of (3,...
a. How many 3-digit numbers can you make using the digits from the set of (3, 4, 5, 6, 8, 2, 7) without repetitions? b. How many numbers can be made with the digits from the set of (0, 3, 5, 7, 8, 4, 9), which are greater than 0 and less than a thousand (repetitions are allowed)?
3) Which of the following statements is true or false a) Character displacement occurs when two...
3) Which of the following statements is true or false a) Character displacement occurs when two species using the same resource overlap in their distributions and become more similar to each other than they are where they don’t overlap. b) If you wanted to know if an organism which expresses a dominant phenotype is homozygous or heterozygous for a trait (e.g., round pea shape, R_), you could do a “test cross” between the unknown individual (R_) and an individual which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT