【发布时间】:2021-03-28 01:50:16
【问题描述】:
我需要创建一个 Java 控制台应用程序,为用户提供食物名称建议。此信息将从sqlite 数据库文件中获取。用户将编写文本,程序将低声说出项目的名称。当用户想要完成文本时,他按 TAB 键,应用程序将获得下一项。
我已经在 ArrayList 中记录了所有数据库项。
My main idea(without everything except for the top text)
部分代码 - 主类
Scanner sc=new Scanner(System.in);
ArrayList<Food> foodList= DatabaseServices.loadAllFood();
ArrayList<String> foodNameList=new ArrayList();
String nameFood;
while (condition) {//when I have all recipe names
//some code
nameFood=sc.nextLine();
foodNameList(nameFood);
}
编辑:
数据库服务类
public ArrayList<food> loadAllFood() {//fetch data and sort them
ArrayList<food> foodList = new ArrayList<>();
try {
String SQL = "SELECT * FROM foods ";
ResultSet data = MyDatabaseConnection.executeQuery(SQL);
do {
Food food= new Food(data.getInt("food_id"),
data.getString("nfood_nazev"),
data.getInt("food_bilkoviny"),
data.getInt("food_sacharidy"),
data.getInt("food_tuky"));
foodList .add(potravina);
} while (data.next());
Collections.sort(foodList );
} catch (SQLException ex) {
Logger.getLogger(DatabaseServices.class.getName()).log(Level.SEVERE, null, ex);
}
return foodList;
}
有什么简单的方法吗???
【问题讨论】:
-
1) 获取数据 2) 对它们进行排序 3) 文本框的 onChange,对列表进行二进制搜索,以查找作为输入的超字符串的最接近匹配项(如果有) 4) 如果有的话,显示它在建议中
-
1),2) 完成 3) 它不应该是文本框,而是 jconsole。我解释得很糟糕:/。我想使用 foreach 和条件的列表搜索,如果有类似的字符串。( string+".*" )
标签: java sqlite autocomplete console-application