I am very new to java. I am working on project for class that would look up books in a text file and display information about them. Primarily if the book is in stock or not. The text file is set up like this: {ISBN, Author, Type, Stock}
我是java的新手。我正在研究课程项目,该课程将在文本文件中查找书籍并显示有关它们的信息。主要是如果这本书有货。文本文件设置如下:{ISBN,作者,类型,股票}
I have coded a user interface that allows the user to type in ISBN, Author, and Type. Ideally, I would like for the user to just search one of these and return the needed information. However, just searching via ISBN would be acceptable for now. My code right now only takes what is typed into the textboxes and displays it in a large textbox. I am somewhat familiar with reading a text file in but have no idea how I would take the text from a textbox and use it to search the file. Any help would be greatly appreciated.
我编写了一个用户界面,允许用户输入ISBN,作者和类型。理想情况下,我希望用户只搜索其中一个并返回所需的信息。但是,现在只需通过ISBN搜索即可。我的代码现在只接受在文本框中输入的内容并将其显示在一个大文本框中。我对阅读文本文件有些熟悉,但不知道如何从文本框中获取文本并使用它来搜索文件。任何帮助将不胜感激。
Here is my code:
这是我的代码:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
public class InventoryInterface extends JFrame
{
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 350;
private JButton btnSearch;
private JButton btnDatabase;
private JButton btnRefresh;
private JLabel lblISBN, lblAuthor, lblType;
private JTextField txtISBN, txtAuthor, txtType;
private JTextArea txtOutput;
public InventoryInterfaceSimple()
{
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void createComponents()
{
btnSearch = new JButton("Search");
lblISBN = new JLabel("ISBN");
lblAuthor = new JLabel("Author");
lblType = new JLabel("Type");
txtISBN = new JTextField(10);
txtAuthor = new JTextField(10);
txtType = new JTextField(10);
txtOutput = new JTextArea(30,30);
txtOutput.setText("");
txtOutput.setEditable(false);
ActionListener action = new InventoryOutput();
btnSearch.addActionListener(action);
JPanel panel = new JPanel();
panel.setLayout(null);
lblISBN.setBounds(10,10,50,25);
txtISBN.setBounds(55,10,125,25);
lblAuthor.setBounds(10,40,50,25);
txtAuthor.setBounds(55,40,125,25);
lblType.setBounds(10,70,50,25);
txtType.setBounds(55,70,125,25);
btnSearch.setBounds(30,130,150,25);
JScrollPane scrollArea = new JScrollPane(txtOutput);
scrollArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollArea.setBounds(200,10,350,200);
panel.add(scrollArea);
panel.add(lblISBN);
panel.add(txtISBN);
panel.add(lblAuthor);
panel.add(txtAuthor);
panel.add(lblType);
panel.add(txtType);
panel.add(btnSearch);
panel.add(scrollArea);
add(panel);
}
class InventoryOutput implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String inventoryString = "";
inventoryString += txtISBN.getText() + " - ";
inventoryString += "Author: " + txtAuthor.getText() + " - ";
inventoryString += "Type: " + txtType.getText() + " - ";
txtOutput.append(inventoryString + "\n");
txtISBN.setText("");
txtAuthor.setText("");
txtType.setText("");
}
}
}
2 个解决方案
#1
you can replace your code with this.. this is a starting point, you can do many improvements to this code, but this will work. change REPOSITORY_FILE_PATH to your data file
你可以用这个代替你的代码..这是一个起点,你可以对这段代码做很多改进,但这样可行。将REPOSITORY_FILE_PATH更改为您的数据文件
class InventoryOutput implements ActionListener {
private final String REPOSITORY_FILE_PATH = "C:\\temp\\book-repo.txt";
private final File REPOSITORY_FILE = new File(REPOSITORY_FILE_PATH);
public void actionPerformed(ActionEvent event) {
String inventoryString = "";
String requestedISBN = txtISBN.getText().trim().toLowerCase();
String requestedAuthor = txtAuthor.getText().trim();
String requestedType = txtType.getText().trim();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(REPOSITORY_FILE));
String line;
while ((line = reader.readLine()) != null) {
String lineLtrim = line.toLowerCase().replaceAll("^\\{", ""); //{
String lineRtrim = lineLtrim.replaceAll("\\}$", ""); //}
String[] data = lineRtrim.split(","); //ISBN, Author, Type, Stock
if (data.length < 4) {
throw new IllegalArgumentException("bad datafile: All fields must be entered: " + line);
}
if (data[0].equals(requestedISBN)) {
inventoryString += txtISBN.getText() + " - Author: " + data[1] + " - Type: " + data[2] + " - Stock:" + data[3];
txtOutput.append(inventoryString + "\n");
return;
}
}
reader.close();
inventoryString += txtISBN.getText() + " - Not Found";
txtOutput.append(inventoryString + "\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
you can just add this main() code to test, ideally you should write unit test-- to cover all cases.
你可以添加这个main()代码进行测试,理想情况下你应该编写单元测试 - 以涵盖所有情况。
public static void main(String[] args) {
JFrame f = new InventoryInterface();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(600, 600));
f.pack();
f.setVisible(true);
}
data file
{ISBN1234,me,drama,5}
{ISBN1235,me,Tech,0}
{ISBN1236,me,Fiction,2}
{ISBN1237,me,Audio/Kids,4}
Not sure which part you did not get,,
不确定你没有得到哪一部分,
- on the listener-- we read the ISBN entered by user
String requestedISBN = txtISBN.getText().trim().toLowerCase();
and convert it in lower case so, we can compare with lower-cased-data - then we read your data file (line by line)
- we then from each line we remove character "{" and "}" // comment says that
- then we get "isbn,author, type, stock" , so we split by ","
- split gives us array of string putting each element in array in order i.e data[0] has isbn, data[1] has author ...etc
- we check if the data file is correct by making sure we have all elements in there i.e
if (data.length < 4) {
- we throw exception if the size of array is not <4 i.e. we found that the data file has incorrect data.
- then we compare if the input isbn (data[0]) is same as one of the line element from your data file
- if we find a match we display it in the textArea and exit the loop,
- if we dont find we display "not Found"
在监听器上 - 我们读取用户输入的ISBN字符串requestedISBN = txtISBN.getText()。trim()。toLowerCase();并将其转换为小写,因此,我们可以与较低的数据进行比较
然后我们读取你的数据文件(逐行)
然后我们从每一行删除字符“{”和“}”//评论说
然后我们得到“isbn,作者,类型,股票”,所以我们分为“,”
split给出了一个字符串数组,按顺序将每个元素放在数组中,即data [0]有isbn,data [1]有作者...等
我们检查数据文件是否正确,确保我们有所有元素,即if(data.length <4){
如果数组的大小不是<4,我们就会抛出异常,即我们发现数据文件的数据不正确。
然后我们比较输入isbn(data [0])是否与数据文件中的一个line元素相同
如果我们找到匹配项,我们会在textArea中显示它并退出循环,
如果我们没有发现我们显示“未找到”
If you wrote the code (listener) yourself, this should be trivial to you.
如果您自己编写代码(监听器),这对您来说应该是微不足道的。
#2
You could create a private String chosenISBN
and set the value of it in the actionPerformed
method. Then you can access the value everywhere in the class. You could even create a getter for the chosenISBN
as well, if you need it.
您可以创建一个私有String selectedNSBN并在actionPerformed方法中设置它的值。然后,您可以访问班级中的任何值。如果需要,您甚至可以为所选的ISBN创建一个getter。
#1
you can replace your code with this.. this is a starting point, you can do many improvements to this code, but this will work. change REPOSITORY_FILE_PATH to your data file
你可以用这个代替你的代码..这是一个起点,你可以对这段代码做很多改进,但这样可行。将REPOSITORY_FILE_PATH更改为您的数据文件
class InventoryOutput implements ActionListener {
private final String REPOSITORY_FILE_PATH = "C:\\temp\\book-repo.txt";
private final File REPOSITORY_FILE = new File(REPOSITORY_FILE_PATH);
public void actionPerformed(ActionEvent event) {
String inventoryString = "";
String requestedISBN = txtISBN.getText().trim().toLowerCase();
String requestedAuthor = txtAuthor.getText().trim();
String requestedType = txtType.getText().trim();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(REPOSITORY_FILE));
String line;
while ((line = reader.readLine()) != null) {
String lineLtrim = line.toLowerCase().replaceAll("^\\{", ""); //{
String lineRtrim = lineLtrim.replaceAll("\\}$", ""); //}
String[] data = lineRtrim.split(","); //ISBN, Author, Type, Stock
if (data.length < 4) {
throw new IllegalArgumentException("bad datafile: All fields must be entered: " + line);
}
if (data[0].equals(requestedISBN)) {
inventoryString += txtISBN.getText() + " - Author: " + data[1] + " - Type: " + data[2] + " - Stock:" + data[3];
txtOutput.append(inventoryString + "\n");
return;
}
}
reader.close();
inventoryString += txtISBN.getText() + " - Not Found";
txtOutput.append(inventoryString + "\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
you can just add this main() code to test, ideally you should write unit test-- to cover all cases.
你可以添加这个main()代码进行测试,理想情况下你应该编写单元测试 - 以涵盖所有情况。
public static void main(String[] args) {
JFrame f = new InventoryInterface();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(600, 600));
f.pack();
f.setVisible(true);
}
data file
{ISBN1234,me,drama,5}
{ISBN1235,me,Tech,0}
{ISBN1236,me,Fiction,2}
{ISBN1237,me,Audio/Kids,4}
Not sure which part you did not get,,
不确定你没有得到哪一部分,
- on the listener-- we read the ISBN entered by user
String requestedISBN = txtISBN.getText().trim().toLowerCase();
and convert it in lower case so, we can compare with lower-cased-data - then we read your data file (line by line)
- we then from each line we remove character "{" and "}" // comment says that
- then we get "isbn,author, type, stock" , so we split by ","
- split gives us array of string putting each element in array in order i.e data[0] has isbn, data[1] has author ...etc
- we check if the data file is correct by making sure we have all elements in there i.e
if (data.length < 4) {
- we throw exception if the size of array is not <4 i.e. we found that the data file has incorrect data.
- then we compare if the input isbn (data[0]) is same as one of the line element from your data file
- if we find a match we display it in the textArea and exit the loop,
- if we dont find we display "not Found"
在监听器上 - 我们读取用户输入的ISBN字符串requestedISBN = txtISBN.getText()。trim()。toLowerCase();并将其转换为小写,因此,我们可以与较低的数据进行比较
然后我们读取你的数据文件(逐行)
然后我们从每一行删除字符“{”和“}”//评论说
然后我们得到“isbn,作者,类型,股票”,所以我们分为“,”
split给出了一个字符串数组,按顺序将每个元素放在数组中,即data [0]有isbn,data [1]有作者...等
我们检查数据文件是否正确,确保我们有所有元素,即if(data.length <4){
如果数组的大小不是<4,我们就会抛出异常,即我们发现数据文件的数据不正确。
然后我们比较输入isbn(data [0])是否与数据文件中的一个line元素相同
如果我们找到匹配项,我们会在textArea中显示它并退出循环,
如果我们没有发现我们显示“未找到”
If you wrote the code (listener) yourself, this should be trivial to you.
如果您自己编写代码(监听器),这对您来说应该是微不足道的。
#2
You could create a private String chosenISBN
and set the value of it in the actionPerformed
method. Then you can access the value everywhere in the class. You could even create a getter for the chosenISBN
as well, if you need it.
您可以创建一个私有String selectedNSBN并在actionPerformed方法中设置它的值。然后,您可以访问班级中的任何值。如果需要,您甚至可以为所选的ISBN创建一个getter。