1

I'm trying to figure out this problem. The directions are set hasDigit to true when a three character passCode from a scanner contains a digit.

Code below

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();

      hasDigit = Character.isDigit(passCode);
     
      
      

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

I've entered in the line hasDigit = Character.isDigit(passCode);

My logic is that character.isDigit is checking the passCode from the scanner, but I keep getting an error.

I've also tried: hasDigit = Character.isDigit(0); and the first test passes, but not the other. From this I assumed that I would have to enter in the string so it would test for anything, not just the character at position 0.

Thanks for the help.

iggylombardi
  • 21
  • 3
  • 5
  • 1
    first step is to look at the javadocs https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isDigit(char) – Scary Wombat Feb 26 '19 at 05:33
  • ...yes...and match that up with javadocs https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html (look at the definition of the next() method) – CryptoFool Feb 26 '19 at 05:35
  • Possible duplicate of [Java String - See if a string contains only numbers and not letters](https://stackoverflow.com/questions/10575624/java-string-see-if-a-string-contains-only-numbers-and-not-letters) – Ammar Ali Feb 26 '19 at 05:54

3 Answers3

1
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();

      if ((Character.isDigit(passCode.charAt(0))) || (Character.isDigit(passCode.charAt(1))) || (Character.isDigit(passCode.charAt(2)))) {
         hasDigit = true;
      }

      if (hasDigit) {
         System.out.println("Has a digit.");
      }
      else {
         System.out.println("Has no digit.");
      }
   }
}
0
for(int i = 0 ;i <passCode.length();i++) {
         hasDigit = Character.isDigit(passCode.charAt(i));
         if (hasDigit) {
             System.out.println("Has a digit.");
             break;
         }
}
if(!hasDigit) {
    System.out.println("Has no digit.");
}

Pity that the answer was revealed but if you are going to spoil the learning process at least try giving the right code :/

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
-2

isDigit() function of Character class excepts either one character or an integer. It does not excepts string , that's why you are getting error. You can modify your code to -

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

         for(int i = 0 ;i <passCode.length();i++) {
             hasDigit = Character.isDigit(passCode.charAt(i));
             if(hasDigit)
               break;
         }
          if (hasDigit) {
             System.out.println("Has a digit.");
          }
          else {
             System.out.println("Has no digit.");
          }

this will give you required result.

prachi
  • 305
  • 1
  • 7