Scanner sc1 = new Scanner(System.in); System.out.println("请输入您要查找的序号:"); String num = sc1.nextLine(); judgeSearch jud = new judgeSearch(); try(FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr)){ if(1 == jud.judge(num)){ while (true) { String line = br.readLine(); String[] str = line.split(" ");//分割 if (str[0].equals(num)) { System.out.println(line); break; } } } if(0 == jud.judge(num)){ System.out.println("没有该商品!"); break; }
时间: 2024-02-10 16:06:36 浏览: 26
这段代码的作用是从文件中查找指定序号的商品信息并输出。
首先,通过 Scanner 类从控制台读取用户输入的要查找的商品序号。
然后,通过自定义的 judgeSearch 类的 judge 方法判断该序号是否存在于文件中。
接着,使用 FileReader 和 BufferedReader 读取文件中的每一行数据,并对每行数据进行分割,判断序号是否匹配输入的序号。如果匹配,则输出该行数据并退出循环;如果不匹配,继续循环查找。
最后,如果输入的序号不存在于文件中,则输出提示信息“没有该商品!”。
需要注意的是,该代码可能会抛出 IOException 异常,需要进行异常处理。
相关问题
怎么优化这串代码Scanner sc1 = new Scanner(System.in); System.out.println("请输入您要查找的序号:"); String num = sc1.nextLine(); judgeSearch jud = new judgeSearch(); try(FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr)){ if(1 == jud.judge(num)){ while (true) { String line = br.readLine(); String[] str = line.split(" ");//分割 if (str[0].equals(num)) { System.out.println(line); break; } } } if(0 == jud.judge(num)){ System.out.println("没有该商品!"); break; }
根据这段代码,我给出以下几点优化建议:
1. 对于输入的序号进行校验,避免用户输入非法数据。比如可以使用正则表达式判断输入是否为数字等有效格式。
2. 考虑到文件较大时,读取文件的效率较低,在读取文件数据时考虑使用缓存来提高读取效率。可以使用 BufferedInputStream 来进行缓存读取文件数据。
3. 可以使用 try-with-resources 语句来自动关闭资源,避免忘记手动关闭资源导致资源泄漏。
4. 可以将读取文件数据的代码封装成一个方法,提高代码的复用性和可读性。
下面是对原代码进行上述优化后的示例代码:
```java
try (Scanner sc1 = new Scanner(System.in);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedReader br = new BufferedReader(new InputStreamReader(bis))) {
System.out.println("请输入您要查找的序号:");
String num = sc1.nextLine();
if (!num.matches("\\d+")) {
System.out.println("输入的序号格式不正确!");
return;
}
if (judgeSearch.judge(num) != 1) {
System.out.println("没有该商品!");
return;
}
String line;
while ((line = br.readLine()) != null) {
String[] str = line.split(" ");
if (str[0].equals(num)) {
System.out.println(line);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
```
需要注意的是,以上代码仅供参考,具体优化方式还需根据实际情况进行调整。
import java.util.ArrayList; import java.util.Scanner; public class ContactManner { public static void main(String[] args) { ArrayList<Contact> array = new ArrayList(); while (true) { Contact s = new Contact(); s.Menu(); Scanner sc1 = new Scanner(System.in); int d = sc1.nextInt(); switch (d) { case 1: { addPeople(array); System.out.println("添加联系人成功"); break; } case 2: { deletePeople(array); break; } case 3: { revisePeople(array); break; } case 4: { checkPeople(array); break; } case 5: { System.out.println("退出成功,谢谢使用"); System.exit(0); break; } default: { System.out.println("错误,请重新输入"); } } } } public static void addPeople(ArrayList<Contact>array) { Scanner sc=new Scanner(System.in); System.out.println("请输入姓名"); String name=sc.nextLine(); System.out.println("请输入电话号码"); String phone=sc.nextLine(); System.out.println("请输入家庭住址"); String address=sc.nextLine(); Contact s1=new Contact(); s1.setAddress(address); s1.setName(name); s1.setPhone(phone); array.add(s1); } public static void deletePeople(ArrayList<Contact>array) { if(array.isEmpty()) { System.out.println("没有联系人信息,请先添加联系人信息再操作"); } else { Scanner sc=new Scanner(System.in); System.out.println("请输入你要删除联系人名字"); String num=sc.nextLine(); int sum=0; for(int i=0;i<array.size();i++) { Contact stu = array.get(i); if(stu.getName().equals(num)) { array.remove(i); sum++; System.out.println("删除成功"); } } if(sum==0){ System.out.println("删除失败,该联系人信息不存在"); } } } public static void revisePeople(ArrayList<Contact>array) { if(array.isEmpty()) { System.out.println("没有联系人信息,请添加后再进行操作"); } else { Scanner sc1 = new Scanner(System.in); System.out.println("请输入要修改的联系人姓名"); String name1 = sc1.nextLine(); int sum = 0; for(int j = 0;j<array.size();j++) { Contact s2 = array.get(j); if(s2.getName().equals(na扩充代码
me1)) { System.out.println("请输入修改后的姓名"); String name2 = sc1.nextLine(); System.out.println("请输入修改后的电话号码"); String phone = sc1.nextLine(); System.out.println("请输入修改后的家庭住址"); String address = sc1.nextLine(); s2.setName(name2); s2.setPhone(phone); s2.setAddress(address); sum++; System.out.println("修改成功"); } } if(sum == 0) { System.out.println("修改失败,该联系人信息不存在"); } } } public static void checkPeople(ArrayList<Contact>array) { if(array.isEmpty()) { System.out.println("没有联系人信息,请添加后再进行操作"); } else { System.out.println("姓名\t\t电话号码\t\t家庭住址"); for(int k = 0;k<array.size();k++) { Contact s3 = array.get(k); System.out.println(s3.getName() + "\t\t" + s3.getPhone() + "\t\t" + s3.getAddress()); } } } }
这段 Java 代码的功能是什么?
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)