2016-12-08 80 views
-1

我對Java非常沒有經驗,我需要幫助我的學校assingment。我不知道如何獲得用戶輸入。代碼已經提供給我們,我們只需要用代碼來代替虛線來完成程序,但我被卡住了。 我有兩個班,他們在這裏。Java編碼數組列表問題

/** 
    * Write a description of class PhoneBookEntry here. 
    * 
    * @author (your name) 
    * @version (a version number or a date) 
    */ 
    public class PhoneBookEntry 
    { 
     private String name;   // Person's name 
     private String phoneNumber; // Person's phone number 

     /** 
     * The constructor initializes the person's name 
     * and phone number. 
     */ 

     public PhoneBookEntry(String n, String pn) 
     { 
      name = n; 
      phoneNumber = pn; 
     } 

     /** 
     * The setName method sets the person's name. 
     */ 

     public void setName(String n) 
     { 
      name = n; 
     } 

     /** 
     * setPhoneNumber method sets the person's 
     * phone number. 
     */ 

     public void setPhoneNumber(String pn) 
     { 
      phoneNumber = pn; 
     } 

     /** 
     * The getName method returns the person's 
     * name. 
     */ 

     public String getName() 
     { 
      return name; 
     } 

     /** 
     * The getPhoneNumber method returns the 
     * person's phone number. 
     */ 

     public String getPhoneNumber() 
     { 
      return phoneNumber; 
     } 
    } 




     /** 

* Chapter 7 
* Lab Assignment: Phone Book ArrayList 
* This program demonstrates the PhoneBookEntry class. 
*/ 

public class PhoneBookDemo 
{ 
    public static void main(String args[]) 
    { 
     // Constant for the numer of entries. 
     final int NUM_ENTRIES = 5; 

     // Create an ArrayList to hold PhoneBookEntry objects. 
     ArrayList<PhoneBookEntry> list = 
     new ArrayList<PhoneBookEntry>(); 

     // Tell the user what's about to happen. 
     System.out.println("I'm going to ask you to enter " + 
         NUM_ENTRIES + " names and phone numbers."); 
     System.out.println(); 

     // Store PhoneBookEntry objects in the ArrayList. 
     for (int i = 0; i < NUM_ENTRIES; i++) 
     { 
     ----------------- 
     System.out.println(); 
     } 

     System.out.println("Here's the data you entered:"); 

     // Display the data stored in the ArrayList. 
     for (int i = 0; i < list.size(); i++) 
     { 
     System.out.println(list); 
     } 
    } 

    /** 
    * The getEntry method creates a PhoneBookEntry object 
    * populated with data entered by the user and returns 
    * a reference to the object. 
    */ 

    public static PhoneBookEntry getEntry() 
    { 
     // Create a Scanner object for keyboard input. 
     Scanner keyboard = new Scanner(System.in); 

     // Variables to hold a person's name and 
     // phone number. 
     String name; 
     String phoneNumber; 

     // Get the data. 
     System.out.print("Enter a person's name: "); 
     name = keyboard.nextLine(); 
     System.out.print("Enter that person's phone number: "); 
     phoneNumber = keyboard.nextLine(); 

     // Create a PhoneBookEntry object. 
     PhoneBookEntry entry = new PhoneBookEntry(name, phoneNumber); 

     // Return a reference to the object. 
     return entry; 
    } 

    /** 
    * The displayEntry method displays the data stored 
    * in a PhoneBookEntry object. 
    */ 

    public static void displayEntry(PhoneBookEntry entry) 
    { 
     System.out.println("------------------------------"); 
     System.out.println("Name: " + entry.getName()); 
     System.out.println("Phone number: " + entry.getPhoneNumber()); 
    } 
} 
+2

谷歌掃描儀,這應該會對你有所幫助 – XtremeBaumer

+1

請參閱http://www.javatpoint.com/Scanner-class –

+0

給定的代碼已經在'PhoneBookDemo.getEntry()'方法中實現了對流的處理。你只能在循環中調用這個'getEntry()'方法,並將返回的值添加到'list'中。 – sanastasiadis

回答

0

在循環(虛線),你只需要添加:

list.add(getEntry()); 

流的處理已經在getEntry()方法來實現。

0

這個東西添加到您的代碼:

主類之外:

import java.util.Scanner; 

主類

Scanner input = new Scanner(System.in); 
System.out.println("Asking for input"); 
String userResponse = input.nextLine(); 

對於int,而不是一個字符串中:

Int userResponse = input.nextInt(); 

程序將暫停並等待輸入。然後,你userResponse現在將包含用戶鍵入的內容

+0

他想要一個數值,而不是Strig值 –

+0

哦。我會修復... – durbnpoisn

+0

他實際上需要一個名稱(字符串)和一個電話號碼(字符串)來創建一個PhoneBookEntry實例。該邏輯已經在'PhoneBookDemo.getEntry()'中實現。 – sanastasiadis