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 5 of 5

Thread: Problem with code recognizing invalid characters after spaces....

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question [RESOLVED!] Problem with code recognizing invalid characters after spaces....

    Hello. I have to write a program that has the user enter a number. As long as the number is a whole number, it is valid. If not, it returns an error message. Well everything works fine if there are no spaces. But if the user types spaces and it has the letter "g" in something after the space, it will return it as valid. This is what I have. Is there any code to either 1) crash the program and return the error message if user enters a space or 2) compare ALL the characters in the user input? Thanks in advance!

    import java.util.Scanner;															
       public class CardNumberValidation {
     
    	/**
    	 * @param args
    	 * @return 
    	 */
     
    		public static void main(String[] args) {									
    			Scanner QWERTY = new Scanner(System.in);								
     
    			System.out.println("Enter the 16 digit number located on" + 			
    					" the front of your card.");
     
    			{ try																	
    			{						
    				 long s = 0;														
    				 s = QWERTY.nextLong();												
     
    				System.out.println("Card number entered is valid!");}				
     
    			catch(Exception e)														
    			{																		
    			    System.err.println("NOT A VALID CARD NUMBER. Please try again.");	
    			}
     
    		}
       }
    }
    Last edited by Eclecstatic; September 22nd, 2012 at 10:16 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with code recognizing invalid characters after spaces....

    Can you post the console from when you execute the code that shows what is typed in?
    I get a java.util.InputMismatchException when I enter a g.
    Also print out the value of s to show what value was read.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Problem with code recognizing invalid characters after spaces....

    try changing your catch parameter to (NumberFormatException e) and see what happens

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with code recognizing invalid characters after spaces....

    Changing the exception class to a more restricted one won't change the code catching an exception. The more general class will catch all the exceptions.
    If the OP is entering is a number followed by space(s) followed by some more stuff.
    The nextLong() method reads and converts the number and leaves the rest (after the space(s)) in Scanner's buffer.
    If the code printed out the value of s the OP would see what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with code recognizing invalid characters after spaces....

    OH I GOT IT! I put the user input into a String using the nextLine command then parsed it. That way it takes that entire line as the input, not just up until a space. Works like a charm now!

    import java.util.Scanner;															
       public class CardDigitValidation {
     
    	/**
    	 * @param args
    	 * @return 
    	 */
     
    		public static void main(String[] args) {									
    			Scanner userInput = new Scanner(System.in);								
     
    			System.out.println("Enter the 16 digit number located on" + 			
    					" the front of your card. NO SPACES PLEASE!");
     
    			 try																	
    			{		
    				String s;
    				s = userInput.nextLine();
    				Long.parseLong(s);
     
    				System.out.println("Card number entered is valid!");
    				 }
     
     
    			catch(Exception e)														
    			{																		
    			    System.err.println("NOT A VALID CARD NUMBER. Please try again.");	
    			}
     
    		}
     
    }

Similar Threads

  1. JAVA code for deleting spaces from Text file
    By Maheshgx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 17th, 2012, 06:26 AM
  2. Eclipse not recognizing my method for class.
    By jerryg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 9th, 2012, 06:52 PM
  3. Replies: 4
    Last Post: February 26th, 2012, 05:36 PM
  4. Trying to create Triangles with Characters and Spaces
    By MJtechie12 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 20th, 2011, 04:20 PM
  5. Strange problem printing Unicode characters
    By sophist42 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 29th, 2011, 10:53 AM