import java.util.Scanner; public class Problem1 { public static void main(String
ID: 3625298 • Letter: I
Question
import java.util.Scanner;public class Problem1 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a social security number in the format DDD-DD-DDDD: ");
String d = input.nextLine();
for(int i = 0; i < d.length(); i++) {
if ((i == 3 || i == 6) && (d.charAt(i) != '-')){
System.out.println("Invalid SSN");
}//end if
else if ((i != 3 || i != 6) && (!Character.isDigit(d.charAt(i)))){
System.out.println("Invalid SSN");
}//end inner else if statement
else {
System.out.println("Valid SSN");
}
}//end for loop
}//end public static void main
}//end Problem1
Explanation / Answer
//Here you go it should be working now, i altered your program a bit. Also btw this statement is the reason why it wasnt working properly last time i believe else if (i != 3 || i != 6)... because if you put || which is or, of course one side will always not equal the int. import java.util.Scanner; public class Problem1 { public static void main(String[] args){ Scanner input = new Scanner(System.in); boolean valid=true; System.out.print("Enter a social security number in the format DDD-DD-DDDD: "); String d = input.nextLine(); for(int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.