Dundalk
Dundalk

Reputation: 11

Creating a method to validate a user id

I am trying to write java code (method) to validate a user id. which prompts user to input an id. If user begins with digit 1 - this will be displayed as invalid!!! I would be grateful for any help! Thanks

public static void main(String[] args) {
    //creating an array of 10   
    int[] num = new int[10];

    //asking user to enter a number        
    Scanner kb = new Scanner(System.in);
    System.out.println("Enter and id number : ");
    num = kb.nextInt();

    //calling method    
    GetValidInteger(num);
}
//method
static void GetValidInteger(int[] num) {
    //looping through the array to get the numbers
    for (int i = 0; i < num.length; i++) {
        //if num at index 0 is 1 print out invalid
        if (num[0] == 1) {
            System.out.println("The number you have entered :" + num + "  starts with 1 and is invalid");
        }

        //else print out valid
        else {
            System.out.println("The number you have entered :" + num + " is valid");
        }
    }
}

I am getting an error: int cannot be converted into int[]! at this line: num = kb.nextInt();

Upvotes: 1

Views: 1034

Answers (2)

Marquise Mery
Marquise Mery

Reputation: 140

You are assigning an int to an array of int. That why you have an error. I do not understand why do you use an array. Use a loop, the user will be asked enter the id till it is correct.

public class Test
{

public static void main(String[] args)
{
    int currentId;

    while (true)
    {
        //asking user to enter a number
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter and id number : ");
        currentId = kb.nextInt();

        //calling method
        if (GetValidInteger(currentId))
        {
            //If the id is valid, you re out from the loop, if not retry
            break;
        }
    }

}

//method
static boolean GetValidInteger(int currentId)
{
    boolean isValidId = false;
    boolean doesIdStartWith1 = String.valueOf(currentId).startsWith("1");
    //if num at index 0 is 1 print out invalid
    if (doesIdStartWith1)
    {
        System.out.println("The number you have entered :" + currentId + "  starts with 1 and is invalid");
    }
    else
    {
        //else print out valid

        System.out.println("The number you have entered :" + currentId + " is valid");
        isValidId = true;
    }
    return isValidId;
}
}

Upvotes: 0

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26946

You are passing an int and not an int[] to the method GetValidInteger.

Change it as follow:

// Renamed with a starting lower case because a starting uppercase char is used
// normally for class and interface names.
public static void getValidInteger(int num) {
   // Consider the string representation of that num
   String str = String.valueOf(num);

   // Get the first character of the string version of the int num
   if (str.charAt(0) == '1') {
       System.out.println("The number you have entered :" + num
                          + "  starts with 1 and is invalid");
   } else {
       System.out.println("The number you have entered :" + num + " is valid");
   }
} 

Upvotes: 1

Related Questions