Results 1 to 7 of 7
  1. #1
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default How to check if enter is integer or not

    Hello, I am getting input from the user, for street number if the user does not enter a valid format such as "22p0" or "ridge street" I want to ask him to enter integer. here is my codes, I want my user enter digit

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Program1
    {
    	   
    	public static void main(String[] args)
    	{
    		String firstName;
    		String middleInitial;
    		char middleInitialChar;
    		String lastName;
    	    String streetNumber;
    		String streetName;
    		String city;
    		String state;
    		int zipCode;
    		
    		Scanner in;
    		in = new Scanner (System.in);
    		
    		System.out.println("Enter your first name: ");
    		firstName = in.nextLine();
    		
    		System.out.println("Enter your middle name: ");	
    		middleInitial = in.nextLine();
    	    middleInitialChar = middleInitial.charAt(0);
    		
    		System.out.println("Enter your last name: ");
    		lastName = in.nextLine();
    		
    		System.out.println("Enter your street number: ");
    		streetNumber = in.nextLine();
    		
    	    try 
    	    	{
    		        int x = Integer.parseInt(streetNumber);
    		        streetNumber = Integer.toString(x);
    	        }
    	    catch (NumberFormatException e) 
    	    	{
    	        	System.out.println("input is not an integer.");
    	        } 
    	    
    		in.nextLine();
    		
    		System.out.println("Enter your street name: ");
    		streetName = in.nextLine(); 
    		
    		System.out.println("Enter your city: ");
    		city = in.nextLine();
    		
    		System.out.println("Enter your state: ");
    		state = in.nextLine();
    		
    		System.out.println("Enter your zip code: ");
    		zipCode = in.nextInt();
    		in.nextLine();
    		
    		System.out.println("+++++++++++++++++++++++++++");
    		System.out.println(firstName + " " + middleInitialChar + ". " + lastName );
    		System.out.println(streetNumber + " " + streetName );
    		System.out.println(city + ", " + state + " " + zipCode );
    	}
    }
    Last edited by hacikho; 09-26-2010 at 01:51 PM.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Do you mean for the Street Name or Street Number?

    When using your code, I receive no problems:
    Java Code:
    	String input = inputScanner.nextLine();
    	try {
    		int x = Integer.parseInt(input);
    		input = Integer.toString(x);
    	} catch (NumberFormatException e) {
    		System.out.println("input is not an integer.");
    	} finally {
    		System.out.println("input = "+input);
    	}
    Java Code:
    run:
    abc
    input is not an integer.
    input = abc
    22p0
    input is not an integer.
    input = 22p0
    220
    input = 220

    What are the values of streetNumber, streetNumber2, and streetName in your code (when entering 22p0)?

  3. #3
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    Java Code:
    I edit my code, check this out. Here is the result;
    Enter your first name: 
    john
    Enter your middle name: 
    william
    Enter your last name: 
    brook
    [B]Enter your street number: 
    troy
    input is not an integer.
    4590[/B]
    Enter your street name: 
    willo dr
    Enter your city: 
    euclid
    Enter your state: 
    new york
    Enter your zip code: 
    90867
    +++++++++++++++++++++++++++
    john w. brook
    troy willo dr
    euclid, new york 90867
    
    Process completed.
    Eventhoug i get warning troy is not an integer but on output I still get troy. not 4590 as a street number

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by hacikho View Post
    Eventhoug i get warning troy is not an integer but on output I still get troy. not 4590 as a street number
    That is just a logic error in your program: although the user didn't enter a number your program doesn't ask for a valid number again (and again).

    kind regards,

    Jos

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you want a bit of code to repeat, you'd better use a loop. Myself, I'd create a boolean called something like inputValid, set it false and use a while (!inputValid) {....}. Then be sure to set it to true if the input is OK.

  6. #6
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    Could you please point out the logic error. and help me to fix it?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    If you would write down the steps of your logic, we'll help you get it right.

Similar Threads

  1. How Enter key act like TAB?
    By mine0926 in forum New To Java
    Replies: 2
    Last Post: 06-03-2010, 06:44 AM
  2. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  3. How to check String confirmation with integer
    By Basit56 in forum New To Java
    Replies: 4
    Last Post: 01-06-2010, 11:35 AM
  4. check if String is an integer?
    By McChill in forum New To Java
    Replies: 5
    Last Post: 05-02-2009, 07:51 PM
  5. Replies: 2
    Last Post: 04-16-2008, 12:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •