Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java编码数组列表故障_Java_Arraylist - Fatal编程技术网

Java编码数组列表故障

Java编码数组列表故障,java,arraylist,Java,Arraylist,我对java非常缺乏经验,我需要帮助我的学业。我不知道如何获得用户输入。代码是提供给我们的,我们只需要用一行代码代替虚线来完成程序,但我被卡住了。 我有两门课,在这里 /** * Write a description of class PhoneBookEntry here. * * @author (your name) * @version (a version number or a date) */ public cla

我对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());
   }
}
/**
*在此处编写PhoneBookEntry类的说明。
* 
*@author(你的名字)
*@version(版本号或日期)
*/
公共类电话簿条目
{
私有字符串名称;//人名
私有字符串phoneNumber;//个人电话号码
/**
*构造函数初始化该人员的名称
*和电话号码。
*/
公用电话簿条目(字符串n,字符串pn)
{
name=n;
电话号码=pn;
}
/**
*setName方法设置人员的姓名。
*/
公共void集合名(字符串n)
{
name=n;
}
/**
*setPhoneNumber方法设置此人的
*电话号码。
*/
公共无效setPhoneNumber(字符串pn)
{
电话号码=pn;
}
/**
*getName方法返回此人的
*名字。
*/
公共字符串getName()
{
返回名称;
}
/**
*getPhoneNumber方法返回
*此人的电话号码。
*/
公共字符串getPhoneNumber()
{
返回电话号码;
}
}
/**
*第七章
*实验任务:电话簿阵列列表
*此程序演示PhoneBookEntry类。
*/
公共类电话簿演示
{
公共静态void main(字符串参数[])
{
//条目数的常量。
最终int NUM_条目=5;
//创建用于保存PhoneBookEntry对象的ArrayList。
ArrayList列表=
新的ArrayList();
//告诉用户将要发生什么。
System.out.println(“我要请您输入”+
NUM_条目+“姓名和电话号码”);
System.out.println();
//在ArrayList中存储PhoneBookEntry对象。
对于(int i=0;i
将这些内容添加到您的代码中:

在主要班级之外:

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现在将包含用户键入的内容。

在循环中(虚线),您只需添加:

list.add(getEntry());

流的处理已经在
getEntry()
方法中实现。

google scanner,这应该有助于您确认给定的代码已经在
PhoneBookDemo.getEntry()方法中实现了流的处理。您应该只在循环中调用此
getEntry()
方法,并在
列表中添加返回值。他需要的是一个数字值,而不是Strig值。他实际上需要一个名称(字符串)和电话号码(字符串)来创建
PhoneBookEntry
实例。该逻辑已在
PhoneBookDemo.getEntry()中实现。