gpt4 book ai didi

java - 上次编译的警告 - 无法读取文本文件/查找目录

转载 作者:行者123 更新时间:2023-11-29 04:34:13 24 4
gpt4 key购买 nike

我需要一些指导来了解为什么我的代码无法正常工作。

我已经写完了,但是在编译代码之前我一直收到警告。

警告是:

"Warnings from last compilation... uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details".

当我编译代码时,类已编译且没有语法错误。

唯一的问题是,当我输入文件名时,它找不到文件,我输入了准确的目录。代码:

import java.io.*;
import java.util.*;
/**
* The purpose of this class is to read data
* (line-by-line) from a file.
*/
public class TextFileReader {
private static List list=new ArrayList(50);
private static List removeList=new ArrayList(50);
private static int min=999;
private static int max=0;
private static String minLine=null;
private static String maxLine=null;
/**
* reads data from a given file line by line, processes
* and then prints it out
* @param fileName the name of the file to be processed
* @return none
*/
public static void processFile(String fileName) throws IOException{
System.out.println("file name: " + fileName);
BufferedReader in
= new BufferedReader(new FileReader(fileName));//read text file
String line;
while ( (line = in.readLine()) != null) // reads every line
getTokens(line);
displayResult();
}

/**
* split a given line into several token
*
* @param line the line to be split
* @return none
*/
public static void getTokens(String line){
StringTokenizer st = new StringTokenizer(line, " ");
int count = st.countTokens();//get total tokens in this line
String firstToken=st.nextToken();
String secondToken = " ";
if(count >= 2)
secondToken=st.nextToken();
if(!Character.isDigit(firstToken.charAt(0)) && (count == 1 || Character.isDigit(secondToken.charAt(0)))){
list.add(line);
}
else{
removeList.add(line);
}
// code to process and print out the result
}

public static void displayResult(){
String minStr ="";
String maxStr = "";
int min = Integer.MAX_VALUE;
int max = 0;
int scoreCount = 0;
int sum = 0;
for(int i=0; i<list.size(); i++){
scoreCount = 0;
StringTokenizer st = new StringTokenizer(list.get(i).toString(), " ");
int count = st.countTokens();//get total tokens in this line
String firstToken=st.nextToken();
sum = 0;
if(st.hasMoreTokens()){
scoreCount++;
sum = sum + Integer.parseInt(st.nextToken());
}
if(sum !=0)
sum = sum/scoreCount;
if(min > sum ){
min = sum;
minStr = list.get(i).toString();
}
if(max < sum){
max = sum;
maxStr = list.get(i).toString();
}
}
System.out.println("MIN: "+minStr);
System.out.println("MAX: "+maxStr);
System.out.println("Excluded Entries:");
for(int i=0; i<removeList.size(); i++){
System.out.println(removeList.get(i).toString());
}
}
// other methods ...
public static void main(String arg[]) throws IOException{
Scanner scan = new Scanner(System.in);
System.out.println("Enter File name :");
String fileName = scan.next();
processFile(fileName);
}
}

文本文件:

alex 10 20 30

2.4 10 20 george

bob 40 60

jane 10

100 jeff

Malcom

Jeff 10 20 30 40

最佳答案

那个 -Xlint 的东西是由这一行引起的:

private static List list=new ArrayList(50);

您正在声明一个 raw 在此处输入。不好的做法;不要那样做。曾经。而且很容易修复:

private static List<String> list = new ArrayList<>(50);

(假设您的列表是字符串列表)

郑重声明:您可以通过使用该额外选项运行 javac 来找到该操作...。这里的重要教训:始终努力实现零警告/错误/任何。编译器给你的任何信息都是有帮助的;并确保摆脱任何警告。

除此之外:我快速运行了你的代码; 打开 文件对我有用。但是随后程序因另一个异常而失败。这样做的原因:

String firstToken=st.nextToken(); // in your getTokens()

问题是:您首先必须检查是否有 token !重点是:您的输入文件中有行;这肯定意味着不是每一行都包含标记。您必须在那里添加支票;例如:

if (line.trim().isEmpty()) {
// the line is empty and should not be processed

关于java - 上次编译的警告 - 无法读取文本文件/查找目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42409325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com