java如何调用display_Java修改run(),通过添加循环并删除对display()的调用!

所以我对Java代码有很大的困难,我应该修改ContactList.java,以便循环提示用户输入一个整数键。如果密钥不是负数,则搜索数组以查找具有该密钥的人员。如果找到Person,则显示Person。如果没有找到该人,则显示一条消息。如果用户输入了否定键,程序应该终止。但不知道在何处实现while循环。此外目前run()抛出IOException。我试着改变run(),以便捕获IOException,并且如果捕获到异常,则显示一条描述性消息并终止。

import java.io.IOException;

import java.net.URL;

import java.util.Scanner;

public class ContactList {

private Person[] theList;

private int n; // the number of Persons in theList

// Returns a Scanner associated with a specific text-based URL

// online.

private Scanner makeScanner() throws IOException {

final String source =

"http://userpages.umbc.edu/~jmartens/courses/is247/hw/05/05.txt";

final URL src = new URL(source);

return new Scanner(src.openStream());

} // makeScanner()

// Return a Person instance based upon data read from the given

// Scanner.

private Person getPerson(final Scanner in) throws FileFormatException {

if (!in.hasNextLine())

return null;

String line = in.nextLine().trim();

int key = Integer.parseInt(line);

String name = in.nextLine().trim();

String mail = in.nextLine().trim().toLowerCase();

if (in.hasNextLine()) {

String empty = in.nextLine().trim(); // skip blank line

if (empty.length() > 0)

throw new FileFormatException("missing blank line");

} // if

return new Person(key, name, mail);

} // getPerson()

// Display the array contents.

private void display() {

for (int i = 0; i < n; ++i)

System.out.println(theList[i]);

} // display()

// Example code to display the contents of the contact list file.

private void run() throws IOException {

theList = new Person[1024];

Scanner in = makeScanner();

int index = 0;

Person p = getPerson(in);

while (p != null) {

theList[index++] = p;

p = getPerson(in);

}

n = index;

display();

} // run()

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

ContactList cl = new ContactList();

cl.run();

} // main()

} // class ContactList

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值