Page 408 - Introduction to Programming with Java: A Problem Solving Approach
P. 408

                374 Chapter 10 Arrays and ArrayLists
 /********************************************************************
*
SpeedDialList.java
Dean & Dean
This program creates a cell phone speed-dial phone number
list and prints the created list.
********************************************************************/
*
*
*
*
import java.util.Scanner;
public class SpeedDialList
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
long[] phoneList;
// list of phone numbers
// number of phone numbers
// an entered phone number
int sizeOfList;
long phoneNum;
System.out.print(
"How many speed-dial numbers would you like to enter? ");
 sizeOfList = stdIn.nextInt();
phoneList = new long[sizeOfList];
}
Create an array with a user-specified size.
Apago PDF Enhancer
for (int i=0; i<sizeOfList; i++) ⎫ {⎪
  System.out.print("Enter phone number: ");
phoneNum = stdIn.nextLong();
phoneList[i] = phoneNum;
// end for
⎪
⎬ ⎪ ⎪ ⎭
Fill the array.
Print the array.
    }
// end class SpeedDialList
}
System.out.println((i + 1) + ". " + phoneList[i]);⎪ } // end for ⎭ // end main
System.out.println("\nSpeed Dial List:");
⎫ { ⎪⎬
for (int i=0; i<sizeOfList; i++)
Sample session:
 How many speed-dial numbers would you like to enter? 2
Enter phone number: 8167412000
Enter phone number: 2024561111
Speed Dial List:
1. 8167412000
2. 2024561111
Figure 10.3 SpeedDialList program that shows how to create, fill, and print an array












































   406   407   408   409   410