我想编写存储汽车信息的程序。您可以在对话框中输入每辆车的品牌和所有者名称。最后,通过将信息写入文本文件,然后从中读取到对话框,可以显示包含所有汽车信息的对话框。我创建了一个方法getInfo,它正确地显示了相应的框。
public static void getInfo() {
boolean done = false;
do {
String brand=JOptionPane.showInputDialog(null, "Brand?");
String name=JOptionPane.showInputDialog(null, "Owner?");
if (brand == null) {
done = true;
} else {
String info ="Car: "+brand+" "+"\nOwner: "+name;
String message = " Do you want to input more cars?";
int buttonClicked = JOptionPane.showConfirmDialog(null, message, "", JOptionPane.YES_NO_OPTION);
done = (buttonClicked == JOptionPane.NO_OPTION);
}
} while (!done);
}
public static void main(String[] args) {
getInfo();
}
我不确定如何将信息添加到文本文件以及如何处理循环。要将信息写入文本文件,我尝试将main方法更改为以下
public static void main(String[] args) throws IOException {
try (PrintWriter outstream = new PrintWriter
(new BufferedWriter
(new FileWriter("cars.txt", true)))) {
getInfo();
outstream.println(info);
}
}
我缺少什么以及如何实现这些功能?
答案 0 :(得分:1)
您可以在循环外定义$email_id = $_GET['id'];
$result = $con->prepare("SELECT * FROM bike_showroom
JOIN car_showroom
JOIN coaching
JOIN college
JOIN electronic
JOIN furniture_showroom
JOIN hospital
JOIN job
JOIN mobile_shop
JOIN pets_shops
JOIN play_school
JOIN real_estate
JOIN services
JOIN shopping_store
JOIN stationary_shops
JOIN sweet_shop
WHERE bike_showroom.ad_id = '".$email_id.
"' AND car_showroom.ad_id = '".$email_id.
"' AND coaching.ad_id = '".$email_id.
"' AND college.ad_id = '".$email_id.
"' AND electronic.ad_id = '".$email_id.
"' AND furniture_showroom.ad_id = '".$email_id.
"' AND hospital.ad_id = '".$email_id.
"' AND job.ad_id = '".$email_id.
"' AND mobile_shop.ad_id = '".$email_id.
"' AND pets_shops.ad_id = '".$email_id.
"' AND play_school.ad_id = '".$email_id.
"' AND real_estate.ad_id = '".$email_id.
"' AND services.ad_id = '".$email_id.
"' AND shopping_store.ad_id = '".$email_id.
"' AND stationary_shops.ad_id = '".$email_id.
"' AND sweet_shop.ad_id = '".$email_id."' ");
$result->execute();
$row = $result->fetch();
for($i=0; $row = $result->fetch(); $i++){
echo $row['title'];
}
:
BufferedWriter
然后在你的循环中:
之后File outputFile = new File("path/to/the/file.txt");
if(!outputFile.exists()){
try {
outputFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedWriter bw = null; //this declares a buffer that writes to some stream.
try{
bw = new BufferedWriter(new FileWriter(outputFile,true)); //here we actually create it, and tell it to write to a file stream, directed at the outputFile file.
}catch(IOException e){
e.printStackTrace(); //this is if the file doesn't exist, which shouldn't happen, but we still need to put this here, because better safe than sorry.
}
做的:
String info ="Car: "+brand+" "+"\nOwner: "+name;
退出循环后,请致电:
String info ="Car: "+brand+" "+"\nOwner: "+name;
try {
bw.write(info);//write the information to the buffer when there's new info.
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
首先,对于每个循环,您正在创建info
的新实例,因此您将丢失先前运行的信息。
在您的main方法中,您正在访问info
,但在该范围内不应该知道它。您可以做的是从info
方法返回getInfo()
。
你应该考虑换行等等,但是第一次尝试应该是它。
答案 2 :(得分:0)
尝试以下方法:
public static void main() {
boolean done = false;
do {
Info info = getInfo();
storeInfo(info);
done = getDone();
} while (!done);
displayInfo();
}
class Info {
private brand;
private owner;
}
现在你可以在每种方法中单独做一些事情。
getInfo调用showInputDialog并返回一个Info对象。
storeInfo将Info对象存储到文本文件中。
getDone调用showInputDialog并返回,如果用户想要退出。
displayInfo打开文本文件并显示信息。
实现displayInfo migth的最简单方法是:
void displayInfo() {
// Create a window with TextBox
// Open the text file
// Read the content of the text file and add it to the TextBox
}