Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: need help for cheking if input is digit ?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default need help for cheking if input is digit ?

    Hi every one so basically i'd like to know which method i can use so that the program checks if the input value is a digit or else outputs a message saying that `the value entered is not a number please retry later`
    please i need an answer ASAP :/
    thank u in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: need help for cheking if input is digit ?

    Moved from Introductions section.
    The Character class has a method to test if a character is a digit.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    meziane (September 28th, 2014)

  4. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help for cheking if input is digit ?

    but mine is not a character its a whole string and it is required that i'm not using loops :/

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: need help for cheking if input is digit ?

    With a String you can use a try{} catch block surrounding a call to the Integer class's parse method which will throw an exception if the String has any non-digit characters.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    meziane (September 28th, 2014)

  7. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help for cheking if input is digit ?

    can i have an example of that with a if statement exception ?? and here is my code i want an exception for my year and my day number so that the program outputs an error message if the input is other than a number

    import java.util.Scanner; // Import Scanner class
    public class DayOfYear
    {


    public static void main(String[] args)
    {

    final int JAN = 31;
    final int FEB;
    final int MAR = 31;
    final int APR = 30;
    final int MAY = 31;
    final int JUN = 30;
    final int JUL = 31; //Set final values for each month of the year
    final int AUG = 31;
    final int SEP = 30;
    final int OCT = 31;
    final int NOV = 30;
    final int DEC = 31;


    Scanner kb = new Scanner(System.in);

    System.out.print("Enter a year: "); //prompt user to input a year value
    int year = kb.nextInt();


    boolean isLeapYear = true; // set value of the variable to true



    if ( year <=1582 ) //first exception: year must be more or equal to 152

    {
    System.out.println("Error: year value must be a positive integer beyond 1582" + "\n");
    }

    else if ( ( year % 4 == 0 )&&( year % 100 != 0) || ( year % 400 == 0) ) // if conditions are true then year is a leap year

    {
    isLeapYear = true ;
    }

    else //else it's not a leap year

    {
    isLeapYear = false;
    }



    System.out.print("Enter a day number in that year: "); //prompt user to enter a day number
    int dayNumber = kb.nextInt();

    if ( (isLeapYear == true) && (( dayNumber > 366) || ( dayNumber < 0)) ) // if it's a leap year and daynumber higher than 366 or negative, ERROR

    {
    System.out.print("Error: day number value must be a positive integer more than 0" +
    "\n" +" and less or equal to 366 (given that " + year + " is a leap year)" + "\n");
    }

    else if ( (isLeapYear == false) && (( dayNumber > 365) || ( dayNumber < 0)) ) // if it's not a leap year and daynumber higher than 365 or negative, ERROR

    {
    System.out.print("Error: day number value must be a positive integer more than 0" +
    "\n" +" and less or equal to 365 (given that " + year + " is not a leap year)" + "\n");
    }


    }
    }

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: need help for cheking if input is digit ?

    Please edit your post and wrap your code with code (not quote) tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    The Scanner class has methods to test what the user has entered before the data is read with one of the next methods. You could use that to test if the user's input is right before reading it into a variable.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    meziane (September 28th, 2014)

  10. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help for cheking if input is digit ?

    Sorry for bothering you it is my first time using this forum :p but thank you for your help rally appreciate it
     
    import java.util.Scanner;				// Import Scanner clas
    public class DayOfYear
    {
     
     
    	public static void main(String[] args)
    	{
     
    				final int JAN = 31;
    				final int FEB;
    				final int MAR = 31;
    				final int APR = 30;
    				final int MAY = 31;
    				final int JUN = 30;
    				final int JUL = 31;							//Set final values for each month of the year
    				final int AUG = 31;
    				final int SEP = 30;
    				final int OCT = 31;
    				final int NOV = 30;
    				final int DEC = 31;
     
     
    		Scanner kb = new Scanner(System.in);
     
    		System.out.print("Enter a year: ");				//prompt user to input a year value		
    		int year = kb.nextInt();			
     
     
    		boolean isLeapYear = true;				// set value of the variable to true 
     
     
     
    		if ( year <=1582 )				//first exception: year must be more or equal to 1582
     
    				{
    					System.out.println("Error: year value must be a positive integer beyond 1582" + "\n");
    				}
     
    		else if ( ( year % 4 == 0 )&&( year % 100 != 0) || ( year % 400 == 0) )			// if conditions are true then year is a leap year
     
    				{
    					isLeapYear = true ;
    				}
     
    		else									//else it's not a leap year
     
    				{
    					isLeapYear = false;
    				}
     
     
     
    			System.out.print("Enter a day number in that year: ");		//prompt user to enter a day number
    			int dayNumber = kb.nextInt();
     
    		if ( (isLeapYear == true) && (( dayNumber > 366) || ( dayNumber < 0)) )			// if it's a leap year and daynumber higher than 366 or negative, ERROR
     
    			{
    				System.out.print("Error: day number value must be a positive integer more than 0" +
    								 "\n" +" and less or equal to 366 (given that " +	year + " is a leap year)" + "\n");
    			}
     
    			else if ( (isLeapYear == false) && (( dayNumber > 365) || ( dayNumber < 0)) )	// if it's not a leap year and daynumber higher than 365 or negative, ERROR		
     
    						{
    							System.out.print("Error: day number value must be a positive integer more than 0" +
    											 "\n" +" and less or equal to 365 (given that " +	year + " is not a leap year)" + "\n");
    			}
     
     
    	}
    }

  11. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help for cheking if input is digit ?

    I have a suggestion there is the .hasNextInt method but i don't really know how to use it. may i have some help please ??

  12. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: need help for cheking if input is digit ?

    Write a small test program that uses the Scanner's hasNextInt() method. Then do some tests:
    Prompt the user for some input,
    have the user enter an int and print out the value returned by the hasNextInt()
    have the user enter an non-int and print out the value returned by the hasNextInt()
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    meziane (September 29th, 2014)

  14. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help for cheking if input is digit ?

    Thank u very much you have been really helpfull i figured out where my error was, basically i needed an System.exit() thats why i wasn't having the error message
    Here is my code maybe it will help someone else
    Scanner kb = new Scanner(System.in);
     
    		System.out.print("Enter a year: ");				//prompt user to input a year value
     
    		boolean inputValidationYear;
     
    		if	(!kb.hasNextInt())
    				{
    					inputValidationYear = false;
    					System.out.println("Error: the year value you entered must be an integer");
    					System.exit(0);
    				}
     
    		else{		inputValidationYear = true;	}
     
    		int year = kb.nextInt();
    Thanks again Norm

  15. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: need help for cheking if input is digit ?

    The code could be more user friendly if it gave the user another chance to enter good data.
    Put the code that reads and tests the user's input in a loop that will exit with good data. If the data is bad, read it with the next method, ignore it and go back and test again for good input.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: February 8th, 2014, 03:59 AM
  2. ISBN-10 enter 9 digit to find the 10th digit
    By cldance5678 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 28th, 2013, 02:52 PM
  3. Replies: 1
    Last Post: February 8th, 2013, 06:37 AM
  4. Timer digit
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 2nd, 2011, 07:07 PM