3 Replies - 733 Views - Last Post: 25 February 2014 - 08:43 PM Rate Topic: -----

#1 redboy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-August 12

Anyone have an idea why im getting unreachable code in my program?

Posted 25 February 2014 - 08:25 PM

import java.util.*;
public class SSN {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);
		System.out.println(" Enter a valid social security number in a valid style DDD-DD-DDDD");
		String SSN = input.nextLine();   // String SSN
	//	SSN.compareTo("123-45-6789");
	//	SSN = "123-45-6789";
		String social = "123-45-6789";
		 
		if (isValid(SSN)) {
			System.out.println("Valid Social Security Number");
			}
			else
				System.out.println("Invalid Social Security Number");
		
		input.close();
	}
	
	private static boolean isValid(String SSN) {   
		
		if(! SSN.matches("\\d{3} - \\ d{2} - \\d{4}"));  //"123-45-6789"
		return false;
		
		boolean social = true;
		for(int i = 0; i < SSN.length(); i++){
			
			switch(SSN.charAt(i)){
			
			case 1:
				if(! Character.isDigit(SSN.charAt(i))) social = false;
				break;
				
			case 2 :
				if(!('-' == SSN.charAt(i))) social = false;
				break;
				
				default :
					break;
			}
		}
		return social;
	 
	}

}

This post has been edited by modi123_1: 25 February 2014 - 08:25 PM
Reason for edit:: highlight your text and click the 'code' button


Is This A Good Question/Topic? 0
  • +

Replies To: Anyone have an idea why im getting unreachable code in my program?

#2 x68zeppelin80x   User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Anyone have an idea why im getting unreachable code in my program?

Posted 25 February 2014 - 08:32 PM

Your problem consists of these 2 line:

if (!SSN.matches("\\d{3} - \\ d{2} - \\d{4}")); //"123-45-6789"
    return false;

Since you placed a semi-colon after your if-conditional, it will return the false below. Just remove the semi-colon and all is good. The reason behind this is that once the if-statement hits a semi-colon it is essentially done checking. This means that the return false is actually not part of that conditional. Thus, false will always be returned in that method.

I know it is easier and perfectly valid to not place braces round a one-line if-condition, but it will alleviate any of these problems in the future.

If your statement was:

if (!SSN.matches("\\d{3} - \\ d{2} - \\d{4}")) {
    return false;
}

You would not have had that problem.

This post has been edited by x68zeppelin80x: 25 February 2014 - 08:44 PM

Was This Post Helpful? 1
  • +
  • -

#3 redboy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-August 12

Re: Anyone have an idea why im getting unreachable code in my program?

Posted 25 February 2014 - 08:40 PM

Great thanks for the quick reply..
Was This Post Helpful? 0
  • +
  • -

#4 x68zeppelin80x   User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Anyone have an idea why im getting unreachable code in my program?

Posted 25 February 2014 - 08:43 PM

Sorry for all the edits, I have been working for 12+ hours today and beginning to fall asleep. If that helped, feel free to press the green plus next to my reply, thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1