在文本文件查找字符串,然后在未找到字符串的情况下字符串添加到文本文件的方法,否则拒绝输入
问题说明
public static void main(String[] args) throws IOException{
if (args.length == 1)
{
BufferedReader bf = new BufferedReader (new FileReader("fruit.txt"));
int linecount = 0;
String line;
//run thur the txt file to check if input exist
while (( line = bf.readLine()) != null)
{
linecount ;
int indexfound = line.indexOf(args[0]);
if (indexfound > -1) {
System.out.println("fruit exist on line " linecount);
System.out.println("add another fruit");
System.exit(0);
} else {
BufferedWriter bw = new BufferedWriter(new FileWriter("fruit.txt", true));
String fruit = "";
fruit = args[0];
bw.write("\r\n" fruit);
System.out.println(fruit "added");
}
}
f.close();
bw.close();
}
I want to have the program to search in a text file fruit.txt check if a fruit already exist in it.
如果存在水果,则提示用户再输入1
if fruit exist then prompt user to input another 1
else add in to the nextline of the text file
这是我到目前为止所得到的. 但我不确定为什么不是我想要的.
This is what I got so far. but I am not sure why it isnt what I wanted.
in my text file it started with 3 fruit
apple
orange
pear
添加浆果后
apple
orange
pear
berry
berry
我加瓜后
apple
orange
pear
berry
berry
melon
melon
melon
melon
正确答案
您只是在第一行中检查水果,如果找不到,您将继续添加它.
You are just checking for the fruit in the first line, and if it is not found, you are continuing to add it.
您需要首先完整地读取文件,以检查每一行是否包含您的水果,然后,如果其中不包含水果,则只需将其转储到其中即可.如果包含,则拒绝它.
You need to read your file completely first, one for checking each line, that it contains your fruit or not, and then if it does not contain, just dump that fruit in it. And if it contains, reject it.
因此,您需要将其他部分移到外面.而不是在找到水果时执行System.exit()
,而是可以将布尔变量设置为true,然后根据布尔变量的值来决定是否添加水果.
So, in your while, you need to move that else part outside. And rather than doing System.exit()
when fruit is found, you can set a boolean variable to true, and then later on, based on the value of boolean variable, you can decide whether to add the fruit or not.
boolean found = false;
while (( line = bf.readLine()) != null) {
linecount ;
int indexfound = line.indexOf(args[0]);
if (indexfound > -1) {
System.out.println("fruit exist on line " linecount);
System.out.println("add another fruit");
found = true;
break;
}
}
if (!found) {
BufferedWriter bw = new BufferedWriter(new FileWriter("fruit.txt", true));
String fruit = "";
fruit = args[0];
bw.write("\r\n" fruit);
System.out.println(fruit "added");
bw.close(); // You need to close it here only.
}
bf.close();
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfjjgc
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20