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.
in .java Write a program that reads an integer with 3 digits and prints each digit...
in .java Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, ... Enter an integer of exactly 3 digits(e.g. 538): 319 9 1 3 Hint: consider what you get from these operations: 319%10 319/10 31%10
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...
Write a java program The last digit of a credit card number is the check digit,...
Write a java program The last digit of a credit card number is the check digit, which protects againts transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Follwing steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT