0
 public static boolean isValidNumber(String a1) 
  {

    String x = ("0123456789");
    boolean valid = false; 

    for (int i = 0; i < 4; i++) {
    char c = a1.charAt(i);

      for (int j = 0; j < 10; j++) {
          if ( c == x.charAt(j)) {
            valid = true;
          }
          else {
            valid = false;
          }
        }
      }

      return valid;
  }

The above method checks to see whether an input of a four character string is composed of the characters 0123456789. However, regardless of what the input is, the method always returns as false.

If I were to change the valid value in the else statement to true, the method would always return as true.

What is the error that I have made in this method?

1

5 Answers 5

2

As soon as you find a non matching character, break the loop otherwise the next matching character will set valid to true.

e.g. "123a456" is considered valid.

for (int j = 0; j < 10; j++) {
    if ( c == x.charAt(j)) {
        valid = true;
    }
    else {
        valid = false;
        break;
    }
}

If for some reason you don't want to break the loop, you could keep an "invalid counter" and make sure that is 0 at the end.

Of course for what you are doing here, Integer.parseInt() might be your best bet ;-)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi John, I've added the break statement as you've proposed. However, I still encounter the same problem, as the method still always returns as false.
You have 2 loops. You need to break out of both. so add if(!valid) { break; } to the end of your outer loop. (or use one of the other solutions from myself or others)
1

a String.equals method will check these two strings in a single statement if you are permitted to use that.

 public static boolean isValidNumber(String a1) 
  {

    String x = ("0123456789");
    return x.equals(a1);
  }

Comments

1

I would rewrite your function as given below,

String x = ("0123456789");
boolean valid = false; 

for (int i = 0; i < 4; i++) {
   char c = a1.charAt(i);

   boolean isCharOK = false;
   for (int j = 0; j < 10; j++) {
      if ( c == x.charAt(j)) {
         isCharOK = true;
         break;
      }
   }

   if (!isCharOK) {
      valid = false;
      break;
   }
}

return valid;

Comments

1

John3136 is quite correct, but I would like to propose even better solution to your whole task:

final static String x = "0123456789";
public static boolean isValidNumber(String a1) {
    for (int i = 0; i < a1.length(); ++i) {
        if (x.indexOf(a1.charAt(i)) == -1) return false;
    }
    return true;
}

In short: the above code "looks up" every character in your parameter string a1 in the string composed of digits. If it can find it, continues. If it can't, it means a1 consist not only digits and returns false. If it passes through all a1 characters then it returns true :)

And as asked and described in the comments - handling of duplicate characters in argument string:

final static String x = "0123456789";
public static boolean isValidNumber(String a1) {
    for (int i = 0; i < a1.length(); ++i) {
        final char currentChar = a1.charAt(i);
        if (x.indexOf(currentChar) == -1 || a1.indexOf(currentChar, i+1) != -1)
            return false;
    }
    return true;
}

The function call a1.indexOf(currentChar, i+1) essentially checks if there is any duplicate character in the rest of the string (from position i+1 and farther). Which means if it will be able to find duplicate char, the method return false :) Hope this helps, here is more info on String.indexOf(int, int) function if you want:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int, int)

2 Comments

Thanks! This code solves my task. However, for the assignment I am doing, I must also add another condition, where the parameter string a1 cannot have any repeating characters. As I'm not familiar with this code, are there any suggestions of how I should go about doing this so that the method returns true only if the string is composed of characters 0123456789 and there are no repeating characters?
@user3363742 well then you just need to test if there are any duplicate symbols in your 'a1' string. Again, probably the easiest way at this moment would be use charAt(i) function one more time in the same IF. But this time you should look for i-th character in the rest of the 'a1' string and if you find another one (which means, it is second), return false, because 'a1' has duplicate characters. Due to limit of characters in comments, I will updated the code in my first post in a matter of seconds :)
0

You can use this one liner function to check for validity of a String as Number using Regular Expression

 public static boolean isValidNumber(String a1) 
 {
     return a1.matches("[\\d]+");
 }

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.