Week 6 Homework 4

 

1. What's the output of the following code segment? Explain.

 

String s="abcd ";

String t=" abcd ";

String u=" ab cd ";

System.out.println(s.trim());

System.out.println(t.trim());

System.out.println(u.trim());

 

abcd

abcd

ab cd

 

2. Write a program that prompts for and accepts a telephone number in the form ddd-ddd-dddd, where d is a digit, and prints it out in the following format: (ddd) ddd-dddd.

 

import java.io.*;

import java.util.*;

 

public class PhoneNumberParser {

 

public static void main (String[] args) throws IOException {

 

     //Variables declared and initialized

     String areaCode;

     String sevenDigit;

     String nineDigit

 

     //Set up input stream

     Scanner stdin = new Scanner(System.in);

 

     //Display Title

     System.out.println ("PHONE NUMBER CONVERTER");

     System.out.println ("Programmed By: Matt Faulkner");

     System.out.println ("CMIS 141 - Programming in Java\n");

     System.out.println ("This prgram will convert an entered phone number in " +

                         "the format ddd-ddd-dddd into the format" +

                         "(ddd) ddd-dddd.");

     System.out.println ("Enter numbers and dashes only or errors will occur.\n");

     System.out.println("Enter a phone number in the format ddd-ddd-dddd where d  

     is a digit.");

 

     nineDigit = stdin.nextLine();

 

     areaCode = nineDigit.substring (0,3);

     sevenDigit = nineDigit.substring (4, 12);

 

     System.out.println ("The converted format of the phone number you entered

     is:");

     System.out.println ("(" + areaCode + ")" + sevenDigit);

 

 

Send me email

Visit my website

 


 

Home | Photos | Favorite Links | Matt's Bio

UMUC | Feedback | Guest book | E-mail me

 

FaulknerWeb.com is designed and managed by Matt Faulkner

Domain Name service provided by Namesecure

Web Service by eDataRack
Copyright © 1998 - 2009 [FaulknerWeb.com]. All rights reserved.
Updated: June 16, 2009 09:35 .