Identifying Number in String Using For Loops

2.4k views Asked by At

I'm trying to identify if there is a number in the input provided using for loops. I've been trying to do it for a long time, and even searched on the second page of Google. I decided to ask for some help. Here's what I've tried so far:

for (c = 0 ; c < 6 ; c = c++)
{
    c = input.charAt (0);

    if (Character.isDigit (c))
    {
        System.out.println ("Input must only contain letters.");
        return;
    }

}

Basically I'm trying to scan through ever letter, and test them individually if they are a letter or a number, which is what I'm having trouble with, since only the first letter is scanned. I've tried typing "c++" in the loop as well. How would I do scan every letter?

Edit: Whoops, that was an mistake. I changed it to c = c + 1 but it didn't work, I changed it back to c++ but it ended up as c = c++.

5

There are 5 answers

1
shmosel On

You can't use c as the index and the value simultaneously. Try this:

for (int i = 0; i < 6; i++) {
    c = input.charAt(i);
    // ...
}
1
CraigR8806 On

This line: c = input.charAt(0); has two problems.

First you probably don't want to be resetting your iteration value c every time the loop executes. So maybe change that to a different variable like so:

char chr = input.charAt(....

Second, you are only grabbing the first char because you are sending 0 to the .charAt() function. Try this:

char chr = input.charAt(c);
if (Character.isDigit (chr))
{
    System.out.println ("Input must only contain letters.");
    return;
}
0
lsnare On

Use a regular expression and Java's Pattern class:

boolean b = Pattern.matches("[0-9]+", input);

If you're unfamiliar with regular expressions, [0-9]+ will find one or more occurrences (the '+' operator) of any digit between 0 and 9 in your string. Then all you need to do is check whether the variable b is true (indicating that the search found a number) or false (there are no numbers in the string).

Additionally, I noticed you're incrementing your loop variable c in the following way:

c = c++

This operation actually has no effect on the value of c. The ++ operator after a variable in an assignment statement means "increment the variable's value by one after you assign it's current value to the other variable. What's happening then is that you are assigning the value of c to itself, incrementing c by one. This incremented value is never saved back to c, so your loop will probably continue infinitely. The proper way to increment your loop variable is as follows:

for (c = 0; c < 6; c++) {
    // ...
}
0
hopeIsTheonlyWeapon On

I have created this boolean function check if that helps.

public static boolean containtsDigits(String input) {
        boolean isDigit = false;
        if(input != null && !input.isEmpty()){
            char[] letterCharArray = input.toCharArray();
            for(int i = 0 ; i < letterCharArray.length ; i++){
                if(Character.isDigit(letterCharArray[i])){
                    isDigit = true;
                    break;
                }
            }
        }

        return isDigit;
    }
2
Farzam Alam On

Try this out:

public void check(String word){

    int n=word.length();  // To Find length of the string.
    for(int c=0;c<n;c++ ){ 
        char ch=word.charAt(c); // for extracting every single element of word

        if(Character.isDigit(ch))  // To check if its Digit
        {
            System.out.println ("Input must only contain letters.");
            break;
        }
    }

    System.out.println ("All Good");
}