-3

The user should give a string, where we should check if all characters are numbers. If all are digits, we will get one message that the password is with digits and move on with the program. If any of them (or all) is not a digit, then the user should enter password again. (E.g. 465486 is acceptable. hell3429384 or sfdkjsfkj is not acceptable)

Apparently, my code is not working correctly, since whether I give a number or a letter to the password, I get the same result. Am I looping something wrong, or am I using the isDigit method wrong?

import java.util.*; 
public class Password {
    public static void main(String[] args) {
        Scanner passn = new Scanner(System.in);
        System.out.println("Give password: "); 
        String pass = passn.nextLine(); /* user enters password*/
        int length = pass.length();
        boolean isadigit; 
        for(int i = 0; i < length; i++) { /*looping each character one by    one*/     
            char character= pass.charAt(i);
            if (Character.isDigit(i)) { /*checking if each character is digit*/
                isadigit = true; /* if it is, give the value to isadigit true*/
                System.out.println("PASSWORD is w digits");
            }
            else {
                System.out.println("PASSWORD w/out digits");
                passn.nextLine();
            }
        }
    }
}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
ToErotimatiko
  • 179
  • 2
  • 3
  • 14
  • 5
    Possible duplicate of [How to check if a String is numeric in Java](https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java) – Kaustubh Khare Nov 06 '17 at 13:04
  • 1
    Please indent code properly before asking people to read it. – khelwood Nov 06 '17 at 13:04
  • You keep checking if your counter variable `i` is a digit, not if what the user entered is a digit. You also mark the password as being only digits after finding just one single digit. – takendarkk Nov 06 '17 at 13:07
  • Hello @khelwood. I'm a very beginner, I'm trying to learn how to indent properly.I'm sorry. – ToErotimatiko Nov 06 '17 at 13:18

8 Answers8

11

If you can use java-8 there is one-liner:

pass.chars().allMatch(Character::isDigit)
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
  • Thank you ! But the required method is isDigit. Thanks – ToErotimatiko Nov 06 '17 at 13:25
  • @Bloubloum, I am sorry I am not sure if I understood your comment. What do you mean by `isDigit` is required? `Character::isDigit` is used in this solution – Anton Balaniuc Nov 06 '17 at 13:33
  • Don't apologize, I should. You are right, I wasn't very good in my explanation. I'm reading through a book to be honest, and it is required to use specific methods to solve the questions. The required is Character.isDigit() kind of. However, this is great for future references, and way more compact! – ToErotimatiko Nov 06 '17 at 13:45
  • `Character::isDigit` is the same as `Character.isDigit()`, but written in lambda notation. – Sync Nov 06 '17 at 14:49
  • 1
    @Synch To be precise, `Character::isDigit` is the same as `x->Character.isDigit(x)`. – user202729 May 12 '18 at 04:52
1

You are not checking each character, but each index value.

Change

if(Character.isDigit(i))

to

if(Character.isDigit(character))

Also: you don't break out of the loop when you encounter a non-digit...

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • Hello F1sh and thank you. That was it . Also, one of the issues is that I get the answer of ''Password is with digit" multiple times. F.e. If I enter 45687 I get 5 answers. How is it possible to get only one message? – ToErotimatiko Nov 06 '17 at 13:28
1

A side note if you are on Android:

You can use TextUtils.isDigitsOnly() directly, e.g.

TextUtils.isDigitsOnly(myString)

And just in case, an empty string is digit only because it doesn't contain any non-digit; also this method is not null-tolerant.

Hai Zhang
  • 5,574
  • 1
  • 44
  • 51
  • @adi OP explicitly asked about digits, not decimal numbers, and the latter would be a different question to answer. – Hai Zhang Nov 14 '19 at 18:11
0

To be honest: You could write code like you did. But it would be better to use a regular expression on that. You want to know, if a certain String only contains digits. Here is the code:

String foo = "My_pwd_is_wrong_123!";
String bar = "123";
boolean foo_ok = foo.matches("^[0-9]*$") // Will be "false"
boolean bar_ok = bar.matches("^[0-9]*$") // Will be "true"
Sauer
  • 1,429
  • 4
  • 17
  • 32
  • Thank you . The thing is , I'm ready through a book where in each homework it asks you to solve a certain question with certain methods in order to learn how to use. Thank you. – ToErotimatiko Nov 06 '17 at 13:17
  • 2
    `String` has no method `match`. It has [`matches`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#matches(java.lang.String)). – khelwood Nov 06 '17 at 13:21
0

Ok, in that case: Your Problem is, that you continue your loop after identifiyng a non-digit in one Password. You need to BREAK your loop, as soon as you find a non-digit in the pwd:

public class TEST {

    public static void main(String[] args) {
        Scanner passn = new Scanner(System.in);
        System.out.println("Give password: "); 
        String pass = passn.nextLine(); /* user enters password*/
        int length = pass.length();

        boolean isadigit = true; 

        for(int i = 0; i<length; i++) { /*looping each character one by    one*/     
            char character= pass.charAt(i);
            if (!Character.isDigit(character)) { /*checking if current character is digit*/
                isadigit = false; /* if it is not, give the value to isadigit false*/
                break;
            }
        }

        if(isadigit)
            System.out.println("PASSWORD is w digits");
        else
            System.out.println("PASSWORD w/out digits");
    }

}
Sauer
  • 1,429
  • 4
  • 17
  • 32
0

Please see this sample implementation

    boolean isDigit = false;

    for (int i = 0; i < length; i++) {     
        char character = pass.charAt(i);

        if (Character.isDigit(character)) {
            isDigit = true;
        } else {
            passn.nextLine();
        }
    }

    if (isDigit) {
        System.out.println("PASSWORD is w digits");
    } else {
        System.out.println("PASSWORD w/out digits");
    }
Sync
  • 3,571
  • 23
  • 30
0

In Kotlin, you can use

text.all { it.isDigit() } 
kjanderson2
  • 1,209
  • 12
  • 23
-1

try to parse the string to Integer. catch the exception

Scanner passn = new Scanner(System.in);
System.out.println("Give password: "); 
String pass = passn.nextLine(); /* user enters password*/
try
{
    Integer.parseInt(pass);
    System.out.println("Digital password!");
}
catch(NumberFormatException nfe){
    System.out.println("without digit password!");
}
raj
  • 5
  • 4
  • I know the saying "better to say sorry than ask for permission", but for user-input i prefer validators over Exceptions – Sauer Nov 06 '17 at 13:36