I want to take the tax number from a user and check if the number that i am getting is correct but i have a problem .When i run the programm if the user first gives a wrong number(a number with less or more than nine digits) i see the message that i wrote which is correct but when the user gives a correct number after some inputs i see the message again and the loop never stops.If anyone can help me i would appreciate it because i'm stuck :)
enter image description here enter image description here
CodePudding user response:
The problem occurs when you get the input the 2nd time inside the while
loop... Beacuse you aren't actually checking again the TIN number.
I advice you to create a function to check if the TIN is valid and use a while loop to continuously get the input
like so:
public static void main(String[] args) {
System.out.println("Give me your TIN number");
Scanner in = new Scanner(System.in);
long TIN = in.nextLong();
//while the input is not valid ("!" negates the boolean) print invalid input and get the input again
while(!isValidTIN(TIN)){
System.out.println("Wrong number! Try Again");
TIN = in.nextLong();
};
// Countinue with your program
System.out.println("Success");
}
public static boolean isValidTIN(long TIN){
int count = 0;
while(TIN > 0){
TIN /= 10; //You can also use TIN /= 10 instead of TIN = TIN / 10
count ;
}
return count == 9;
}
Also paste the code next time instead of an image