Question

Toll-Free numbers have become so popular that the 800-code prefix has run out of available numbers,...

Toll-Free numbers have become so popular that the 800-code prefix has run out of available numbers, so the additional 3-digit prefix 877 has been added. Create a program in java that accepts a 10-digit phone number (no 1 in front) and tells the user if the number is toll-free. Display Not toll-free otherwise. EXAMPLE Input: 8001234567 Toll-free number

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation::

  • Code in JAVA is given below
  • Please read comments for better understanding of the code
  • Screenshots of the OUTPUT are given at the end of the code


Code in JAVA::

import java.util.Scanner;

public class Toll {

   public static void main(String[] args) {
       /**
       * Using Scanner class object named sc to take input from the user
       * */
       Scanner sc=new Scanner(System.in);
      
       /**
       * A String variable named number is declared below
       * */
       String number;
      
       /**
       * Prompting user to enter the String number
       * */
       System.out.print("Enter the number : ");
       number=sc.nextLine();
       /**
       * Following while loop checks the validity of the String entered by user
       * Check 1: If length is exactly 10 characters long
       * Check 2: There is no '1' in front
       * */
       while(true) {
           if(number.length()==10 && number.charAt(0)!='1') {
               break;
           }
           System.out.println("Invalid Input!");
           System.out.print("\nEnter the number : ");
           number=sc.nextLine();
       }
       /**
       * Now we check if first three characters are "800" or not
       * Using substring() method we can get the substring of String number
       * */
       String value=number.substring(0, 3);
       if(value.equals("800")) {
           System.out.println("Toll-free number");
       }else {
           System.out.println("Not toll-free otherwise");
       }
      
   }

}

OUTPUT::
TEST CASE 1:


TEST CASE 2:


Please provide the feedback!!

Thank You!!


Add a comment
Know the answer?
Add Answer to:
Toll-Free numbers have become so popular that the 800-code prefix has run out of available numbers,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • Code in Java using ( NetBeans ) Chapter 7 Assignment (Popular Candy) - 15 points Your...

    Code in Java using ( NetBeans ) Chapter 7 Assignment (Popular Candy) - 15 points Your goal is to record the sales for 4 different types of candy and determine the total sales and the names of the highest and lowest selling candies. . . Create a String array that stores four different types of candy. Pick whatever candy you would like. (3 points) Have the program prompt the user to enter the number of candy boxes sold for each...

  • JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with...

    JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with individual digits from 0 to 9 inclusive. (i.e. 12345, 09382, 33044, etc.) The player then tries to guess the number. With each guess the program displays two numbers, the first is the number of correct digits that are in the proper position and the second number is the sum of the correct digits. When the user enters the correct five-digit number the program returns...

  • Create a class called Play that has an InputReader as an instance variable. Be sure to...

    Create a class called Play that has an InputReader as an instance variable. Be sure to initialize it in the constructor. The class has two methods. Write a method with this signature: Write a method with this signature: public void stringPlay() The method prompts the user for a string, reads it in, and then displays the string as many times as the length of that string. The output string should be formatted with the first letter uppercase and the rest...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

  • VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON...

    VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON This is a code that is supposed to help someone study/ practice for jeopardy. The two functions described below are required for this assignment. You may add other functions that you think are appropriate: menu() This function, which displays all the user options to the screen, prompt the user for their choice and returns their choice. This function will verify user input and ALWAYS...

  • Please post the code in c++! Computer Labs Write a Computer Labs program to store the...

    Please post the code in c++! Computer Labs Write a Computer Labs program to store the list of user IDs for each computer station using a linked list. Problem Description: You run four computer labs. Each lab contains computer stations that are numbered as shown in the table below: Lab Number Computer station Numbers 1-5 2 1-6 1-4 4 1-3 Each user has a unique five-digit ID number. Whenever a user logs on, the User's ID, labnumber, and the computer...

  • Need to solve this program which will run exactly like sample run .. Thanks Project 2...

    Need to solve this program which will run exactly like sample run .. Thanks Project 2 Using Classes and Objects (Chapter 3) Duc: sec calendar Two files to be submitted: . Algorithm of method main (AlgorithmOfMain.txt, no credit if this is missing) Java Source code (CarpetBill.java- no credit if this has syntax errors) Write a program that creates customers' bills for a carpet company when the information below is given: irst name .Length and width of the carpet in feet...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT