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

Thread: checking for empty strings?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default checking for empty strings?

    I'm trying to check for empty strings with a Scanner and I'm not sure how to loop it so that it asks for the correct input.

    So I want to use
    System.out.print("Enter your First Name: ");
    String employeeFirstName = j.next(); //first name declared
    System.out.print("Enter your Last Name: ");
    String employeeLastName = j.next(); //last name declared

    and I want to have it check so if the user enters a number or a blank that it asks for the input again. I tried using both if and while loops and it would still print "enter last name" after the first name and then also say it was invalid and crash the program.


  2. #2
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: checking for empty strings?

    You need to use try, catch blocks for this.
     public static void main(String[] args) throws InputMismatchException
          {    boolean flag = true;
               int i;
               Scanner j = new Scanner(System.in);
               String employeeFirstName = null;
          do{   
        	  try
              {        	     
     
                System.out.print("Enter your First Name: ");
                employeeFirstName = j.next(); //first name declared
                for(i=0;i<employeeFirstName.length();i++)
                   	if(!((employeeFirstName.charAt(i)>= (int) 'A') && (employeeFirstName.charAt(i)<= (int)'z')))
                		throw new InputMismatchException();
     
     
                flag=!flag;
              }
     
              catch(InputMismatchException e)
              {
            	  System.out.println("Invalid input.Try again");
              }
             }while(flag);
     
          do{   
        	  try
              {        	     
        		  System.out.print("Enter your Last Name: ");
        		  String employeeLastName = j.next(); //last name declared
        		    for(i=0;i<employeeLastName.length();i++)
        		      	if(!(employeeLastName.charAt(i)>= (int) 'A') || !(employeeLastName.charAt(i)<= (int)'z'))
        		    		throw new InputMismatchException();
                       System.out.println("Your name is: "+employeeFirstName+" "+employeeLastName);
                   flag=!flag;
              }
     
              catch(InputMismatchException e)
              {
            	  System.out.println("Invalid input.Try again");
              }
             }while(!flag);
          }
    Last edited by helloworld922; October 31st, 2010 at 09:49 PM.

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: checking for empty strings?

    That is probably proper, but shouldn't this work also...since you are using a boolean in yours anyway, why use the catch blocks?

    boolean goodInput = false;

    while(!goodInput) {
    	goodInput = true;
            System.out.print("Enter first name: ");
    	employeeFirstName = j.next();
    //Nice for loop to ensure all chars are letters
    	for(i=0;i<employeeFirstName.length();i++) {
                    if(!((employeeFirstName.charAt(i)>= (int) 'A') && (employeeFirstName.charAt(i)<= (int)'z'))) {
    		    goodInput = false;
    		    System.out.println("Bad input, try again.");
                        continue;
                    }
    	}
    }
    And then another for last name, resetting goodInput to false of course.

  4. #4
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: checking for empty strings?

    This is a better code than mine. But mine would work too. Thanks for showing an aliter.

Similar Threads

  1. empty response received
    By grashmi13 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 16th, 2010, 05:30 PM
  2. Problem with updating empty JTable
    By byubi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 15th, 2010, 02:06 AM
  3. How to show empty directories in JTree ?
    By ni4ni in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2010, 12:55 AM
  4. [SOLVED] Supressing empty lines and does not do until the end! Why ?
    By lumpy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2010, 07:38 AM
  5. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM