How can I code to restrict the users input to 5 numbers and no more or less....I can't think of a good way to do it.

import java.io.*;

public class Palindrome  {
      public static void main(String [] args){
      try{
      BufferedReader object = new BufferedReader(
                  new InputStreamReader(System.in));
      System.out.println("Enter number");
      int num= Integer.parseInt(object.readLine());
        int n = num;
        int rev=0;
        System.out.println("Number: ");
        System.out.println(" "+ num);
        for (int i=0; i<=num; i++){
          int r=num%10;
          num=num/10;
          rev=rev*10+r;
          i=0;
        }
        System.out.println("After reversing the number: "+ " ");
        System.out.println(" "+ rev);      
        if(n == rev){
        System.out.print("Number is palindrome!");
        }
        else{
        System.out.println("Number is not palindrome!");
        }
      }
      catch(Exception e){
        System.out.println("Out of range!");
      }
  }
}

code to restrict the users input to 5 numbers

I don't think you can restrict then number of characters a user types in at the console.
Are you asking how to keep a user from entering: 23456789
You can't

Can you explain what the problem is?

Member Avatar for coil

You can't stop the user from pressing keys at the console, but you can accomplish essentially the same thing in two ways:

1. Easy way - Just read in input and chop off anything past the 5th number.
2. Hard way - You don't need to do this if it's just an exercise, but you could create a JFrame, have the user enter the number through a formatted text field.

I don't think you can restrict then number of characters a user types in at the console.
Are you asking how to keep a user from entering: 23456789
You can't

Can you explain what the problem is?

Yeah, I was looking to see if there was a way to maybe give the user a message that if the input is greater than 5 numbers, they would have to input a set of numbers less than 5. I know I can't restrict how many they enter, but how can I "suggest" that they enter no more than 5?

how can I "suggest" that they enter no more than 5?

Use: System.out.println

But that doesn't kick back an error if the input is 5 ints or greater....

What does "kick back an error" mean?
Your "get the number" logic should not return the given number until it has tested that the number is what you want. If it gets a bad number, tell the user its bad and ask him to enter another one. Don't return the number until it is ok.

Loop:
  >> Read in string from user
  >> Check length of string
  >> If length == 5:
       >> Parse the integer from the string (if you're dealing with integers)
       >> Exit loop

Above is the pseudocode for how I would approach the problem. If you use a loop, the program will keep prompting the user to enter a string until one with 5 characters is entered, effectively restricting it to 5 characters.

kvass's way is feasible.
Check the input digits' string to see if its length is 5 nor not. If not then ask client to re-enter a 5 digits' number.
There is no way to restrict number of characters a user types in at the console during typing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.