我正在努力做一些功课。除了这个问题,我已解决了所有问题。在搜索网站后,我决定只问自己的问题。
该代码旨在制作一个小菜单,询问用户是否要将电子邮件输入数据库,搜索以前输入的电子邮件或退出。
当我尝试进行二进制搜索以确定电子邮件是否已存在于数据库中时,会出现问题。
错误发生在addNewEmail方法的第二行(最后一行)。 提前感谢您的帮助。
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class project14_Brady {
private static final int ADD = 1;
private static final int SEARCH = 2;
private static final int QUIT = 3;
public static void main(String[] args)
{
ArrayList<String> emailAddresses = new ArrayList<String>();
int size = 0;
Scanner input = new Scanner(System.in);
// Priming read
int menu = menuChoice(input);
while(menu != QUIT)
{
if (menu == ADD)
{
System.out.println("Enter the email address");
String email = input.nextLine();
size = addNewEmail(emailAddresses, size, email);
}
else if (menu == SEARCH)
{
String email = autoComplete(emailAddresses, size, input);
if (email != null)
System.out.println("Found: " + email);
else
System.out.println("No matching email was found");
}
else
{
System.out.println("Unanticipated case");
}
// priming read
menu = menuChoice(input);
} // end while
}
public static String autoComplete(ArrayList<String> data, int size, Scanner input)
{
System.out.println("Enter the first letters, one at a time");
String start = "";
while (true)
{
String read = input.nextLine();
start += read;
System.out.println("DEBUG: " + start);
int count=0;
String result="";
for (int i=0; i<size; ++i)
{
System.out.println("DEBUG: " + data.get(i));
if (data.get(i).startsWith(start))
{
result = data.get(i); // keep just the last one
System.out.println(data.get(i));
++count;
}
}
if (count == 1)
return result;
if (count == 0)
return null;
}
}
public static int menuChoice(Scanner keyboard)
{
System.out.println("Please choose from the following menu of choices:");
System.out.println("1. Enter a new email address");
System.out.println("2. Find an existing email address");
System.out.println("3. Quit.");
System.out.println("What is your choice?");
int choice = keyboard.nextInt();
keyboard.nextLine(); // get rid of newline
// Allow the user to re-enter data
while (choice < ADD || choice > QUIT)
{
System.out.println("You must choose a value between 1 and 3");
System.out.println("Please re-enter your choice");
choice = keyboard.nextInt();
keyboard.nextLine();
}
return choice;
}
public static int addNewEmail(ArrayList<String> data, int size, String insertMe)
{
if (data.binarySearch(data, 0, size, insertMe) > 0)
{
System.out.println("That email address has already been inserted");
return size; // already in array
}
if (size == data.size())
{
System.out.println("Too many addresses are stored");
return size; // the array is full already
}
// This is essentially one inner loop of insertion sort
int index;
for (index = size; index > 0 && data.get(index-1).compareTo(insertMe) > 0; --index)
{
data.add(index, data.get(index-1));
}
data.add(index, insertMe);
return size+1;
}
}
答案 0 :(得分:1)
ArrayList
没有binarySearch
方法。你可以试试这个
if (Collections.binarySearch(data, insertMe) > 0)
这是编译,但我没有详细阅读你的代码,说它绝对是你想要做的。