java txt 项目的最佳解决方案
best solution for java txt project
喜欢的主题说我正在寻找最好的方法:
我得到了 .txt 文件。在这个文件中有例如:
Matthew Sawicki 25\n
Wladimir Putingo 28\n
Barracko Obamaso 27
编写打开此文件、检查最大数字然后打印出来的程序的最佳方法是什么?
我在考虑:打开文件 -> 使用 hasNextLine 方法检查每一行,保存最大数字(用于测量行的插件 i - 1、2、3),然后关闭文件并再次打开,然后以某种方式打印出来那条线
好的,那就去编辑吧。
顺便说一下,我必须在控制台中输入文件名才能打开它。而且我必须使用 Scanner
。
我的代码:
Scanner scanner= new Scanner (System.in);
File file = new File (scanner.nextLine);
scanner = new Scanner (file);
int temp=O;
int i=0;
while (scanner.hasNextLine) {
String word1=scanner.next;
String word2=scanner.next;
String word3=scanner.next;
If(word3>temp)
temp3=word;
i++; // now i get the i id of the line with the biggest number
现在我正在考虑重新打开文件并再次循环以打印出数字最大的那一行(例如 if(newWord3==temp))
这是个好主意吗?以及如何重新打开文件?谁能继续代码?
假设此文件将始终采用相同的格式,这里有一个片段可以执行您想要的操作,无需检查以确保任何内容都出错place/format.
//Create a new scanner pointed at the file in question
Scanner scanner= new Scanner(new File ("C:\Path\To\something.txt"));
//Create a placeholder for the currently known biggest integer
int biggest = 0;
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
//This line assumes that the third word separated by spaces is an
// integer and copies it to the `cndt` variable
int cndt = Integer.parseInt(s.split(" ")[2]);
//If cndt is bigger than the biggest recorded integer so far then
// copy that as the new biggest integer
biggest = cndt > biggest ? cndt : biggest;
}
//Voila
System.out.println("Biggest: " + biggest);
您需要验证相关号码是否存在,以及您是否可以处理文本文件中一行格式错误的情况
有多种方法可以满足您的需求。您描述的方式可以工作,但正如您所指出的,它需要扫描文件两次(在最坏的情况下,即您必须打印的行是最后一行)。
避免再次重新打开文件的更好方法是修改您的算法,不仅保存最大的数字和相应的行号,而且如果数字大于先前保存的数字,则保存整行。然后,当你完成扫描文件时,你可以只打印你保存的字符串,这是包含最大数字的字符串。
请注意,您的代码将不起作用:if 将 String
与 int
进行比较,而且没有 temp3
变量(这可能只是一个错字)。
按照我的建议,你应该有这样的东西:
int rowNumber = Integer.parseInt(word3);
if(rowNumber > temp) {
temp = rowNumber;
tempRow = word1 + " " + word2 + " " + word3;
}
然后你可以打印出 tempRow
(你应该在 while
循环之外定义)。
现在一切都很好。我在我的文件中犯了一个简单的错误(最后一个空输入)所以我不知道该怎么做。
感谢您的努力和 gl!
喜欢的主题说我正在寻找最好的方法:
我得到了 .txt 文件。在这个文件中有例如:
Matthew Sawicki 25\n
Wladimir Putingo 28\n
Barracko Obamaso 27
编写打开此文件、检查最大数字然后打印出来的程序的最佳方法是什么?
我在考虑:打开文件 -> 使用 hasNextLine 方法检查每一行,保存最大数字(用于测量行的插件 i - 1、2、3),然后关闭文件并再次打开,然后以某种方式打印出来那条线
好的,那就去编辑吧。
顺便说一下,我必须在控制台中输入文件名才能打开它。而且我必须使用 Scanner
。
我的代码:
Scanner scanner= new Scanner (System.in);
File file = new File (scanner.nextLine);
scanner = new Scanner (file);
int temp=O;
int i=0;
while (scanner.hasNextLine) {
String word1=scanner.next;
String word2=scanner.next;
String word3=scanner.next;
If(word3>temp)
temp3=word;
i++; // now i get the i id of the line with the biggest number
现在我正在考虑重新打开文件并再次循环以打印出数字最大的那一行(例如 if(newWord3==temp))
这是个好主意吗?以及如何重新打开文件?谁能继续代码?
假设此文件将始终采用相同的格式,这里有一个片段可以执行您想要的操作,无需检查以确保任何内容都出错place/format.
//Create a new scanner pointed at the file in question
Scanner scanner= new Scanner(new File ("C:\Path\To\something.txt"));
//Create a placeholder for the currently known biggest integer
int biggest = 0;
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
//This line assumes that the third word separated by spaces is an
// integer and copies it to the `cndt` variable
int cndt = Integer.parseInt(s.split(" ")[2]);
//If cndt is bigger than the biggest recorded integer so far then
// copy that as the new biggest integer
biggest = cndt > biggest ? cndt : biggest;
}
//Voila
System.out.println("Biggest: " + biggest);
您需要验证相关号码是否存在,以及您是否可以处理文本文件中一行格式错误的情况
有多种方法可以满足您的需求。您描述的方式可以工作,但正如您所指出的,它需要扫描文件两次(在最坏的情况下,即您必须打印的行是最后一行)。
避免再次重新打开文件的更好方法是修改您的算法,不仅保存最大的数字和相应的行号,而且如果数字大于先前保存的数字,则保存整行。然后,当你完成扫描文件时,你可以只打印你保存的字符串,这是包含最大数字的字符串。
请注意,您的代码将不起作用:if 将 String
与 int
进行比较,而且没有 temp3
变量(这可能只是一个错字)。
按照我的建议,你应该有这样的东西:
int rowNumber = Integer.parseInt(word3);
if(rowNumber > temp) {
temp = rowNumber;
tempRow = word1 + " " + word2 + " " + word3;
}
然后你可以打印出 tempRow
(你应该在 while
循环之外定义)。
现在一切都很好。我在我的文件中犯了一个简单的错误(最后一个空输入)所以我不知道该怎么做。 感谢您的努力和 gl!