I have an error output in my code and was wondering if you could help me fix it
ID: 3839448 • Letter: I
Question
I have an error output in my code and was wondering if you could help me fix it here is my code:
import java.util.Scanner;
public class Lab3d {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String phoneNum = "";
System.out.println("Enter 10-digit telephone number: ");
phoneNum = scan.next();
if(phoneNum.length() != 10){
System.out.println("Please enter a 10-digit number");
}
else{
System.out.println("You entered "+phoneNum);
String areaCode = phoneNum.substring(0,3);
String exchangeCode = phoneNum.substring(3,6);
String number = phoneNum.substring(6,10);
System.out.println("The area code is "+areaCode);
System.out.println("The exchange is "+exchangeCode);
System.out.println("The number is "+number);
System.out.println("The complete telephone number is ("+areaCode+") "+exchangeCode+"-"+number);
}
}
}
3. Compare output 111222333 Input Enter 10-digit telephone number: Your output Please enter a 10-digit number Enter 10-digit telephone number You entered 111222333 Expected output Please enter a 10-digit number 4. Compare output 111222333444 Input Enter 10-digit telephone number Your output Please enter a number Enter 10-digit telephone number: You entered 111222 33 34 44 Expected output Please enter a 10-digit number DOWNLOAD SUBMISSION 0/3 0/2Explanation / Answer
Hi
I have fixed the issue and highlighted the code changes below.
Lab3d.java
import java.util.Scanner;
public class Lab3d {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String phoneNum = "";
System.out.println("Enter 10-digit telephone number: ");
phoneNum = scan.next();
System.out.println("You entered "+phoneNum);
if(phoneNum.length() != 10){
System.out.println("Please enter a 10-digit number.");
}
else{
String areaCode = phoneNum.substring(0,3);
String exchangeCode = phoneNum.substring(3,6);
String number = phoneNum.substring(6,10);
System.out.println("The area code is "+areaCode);
System.out.println("The exchange is "+exchangeCode);
System.out.println("The number is "+number);
System.out.println("The complete telephone number is ("+areaCode+") "+exchangeCode+"-"+number);
}
}
}
Output:
Enter 10-digit telephone number:
1234567890
You entered 1234567890
The area code is 123
The exchange is 456
The number is 7890
The complete telephone number is (123) 456-7890
Enter 10-digit telephone number:
111222333444
You entered 111222333444
Please enter a 10-digit number.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.