Java修改run(),方法是添加循环并删除对display()的调用!
所以我在Java代码方面遇到了很多困难,我想修改ContactList.Java,这样它就可以循环提示用户输入一个整数键。如果密钥为非负,则在数组中搜索具有该密钥的人员。如果找到此人,则显示此人。如果找不到此人,则显示相应的消息。如果用户输入负数键,程序应终止。但不确定在哪里实现while循环。当前,run()也抛出IOException。我正在尝试更改run(),以便它捕获IOException,如果捕获到异常,则显示一条描述性消息并终止Java修改run(),方法是添加循环并删除对display()的调用!,java,arrays,while-loop,Java,Arrays,While Loop,所以我在Java代码方面遇到了很多困难,我想修改ContactList.Java,这样它就可以循环提示用户输入一个整数键。如果密钥为非负,则在数组中搜索具有该密钥的人员。如果找到此人,则显示此人。如果找不到此人,则显示相应的消息。如果用户输入负数键,程序应终止。但不确定在哪里实现while循环。当前,run()也抛出IOException。我正在尝试更改run(),以便它捕获IOException,如果捕获到异常,则显示一条描述性消息并终止 import java.io.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
import java.io.IOException;
导入java.net.URL;
导入java.util.Scanner;
公共类联系人列表{
私人[]名单;
private int n;//列表中的人数
//返回与特定基于文本的URL关联的扫描程序
//在线。
专用扫描程序makeScanner()引发IOException{
最终字符串源=
"http://userpages.umbc.edu/~jmartens/courses/is247/hw/05/05.txt”;
最终URL src=新URL(源);
返回新扫描仪(src.openStream());
}//makeScanner()
//根据从给定数据库读取的数据返回Person实例
//扫描仪。
private Person getPerson(中的最终扫描程序)引发FileFormatException{
如果(!in.hasNextLine())
返回null;
字符串行=in.nextLine().trim();
int key=Integer.parseInt(行);
字符串名称=in.nextLine().trim();
字符串mail=in.nextLine().trim().toLowerCase();
if(在.hasNextLine()中){
String empty=in.nextLine().trim();//跳过空行
if(empty.length()>0)
抛出新的FileFormatException(“缺少空行”);
}//如果
返回新人员(密钥、姓名、邮件);
}//getPerson()
//显示数组内容。
专用void display(){
对于(int i=0;i
你知道try/catch吗
private void run() {
try {
// Loop, etc.
} catch (IOException e) {
// Handle exception
}
}
你知道试一试吗
private void run() {
try {
// Loop, etc.
} catch (IOException e) {
// Handle exception
}
}
在
run()
方法中读取整个列表后,可以放置它:
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();
int key = 0;
do {
System.out.print("Enter a key: ");
try {
Scanner userInput = new Scanner(System.in);
key = userInput.nextInt();
} catch (InputMismatchException e) {
System.out.println("> Invalid key");
continue;
}
Person found = search(key);
if (found == null)
System.out.println("> Person not found");
else
System.out.println("> Person found => " + found);
} while(key >= 0);
} // run()
search
方法是一个简单的循环,将列表中每个Person
的id与提供的键进行比较
对于不抛出
IOException
的run
方法,请按照小册子的答案进行操作。一旦在run()方法中读取了整个列表,就可以将其放入:
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();
int key = 0;
do {
System.out.print("Enter a key: ");
try {
Scanner userInput = new Scanner(System.in);
key = userInput.nextInt();
} catch (InputMismatchException e) {
System.out.println("> Invalid key");
continue;
}
Person found = search(key);
if (found == null)
System.out.println("> Person not found");
else
System.out.println("> Person found => " + found);
} while(key >= 0);
} // run()
search
方法是一个简单的循环,将列表中每个Person
的id与提供的键进行比较
对于不抛出IOException
的run
方法,请按照小册子的答案进行操作。如果这是作业,请将其标记为作业。请将标题更改为更有意义的内容。这将增加你获得有用答案的机会。如果这是作业,请将其标记为作业。请将标题更改为更有意义的内容。这会增加你得到有用答案的机会。