gpt4 book ai didi

java - 将文本文件读入数组的字符串中

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:29 25 4
gpt4 key购买 nike

我正在尝试将此文件读入 Java。我需要计算调整后和未调整的 GPA。这是文件:

111,English 1 Honors,84,5-
180,Freshman Art,91,1.5-
210,Latin Honors 1,96,5-
313,Geometry Honors,86,5-
431,Religion 1-Catholic Christianity,89,5-
511,Biology Honors,88,5-
515,Biology Honors Lab,A,0.5-
611,World History Honors,90,5-
711,Freshman Computers Applications,98,1.5-
810,Phys Ed- Freshman,A,1.5-
120,English 2 CP,84,5-
181,Sophomore Art,95,1.5-
212,Latin 2 Honors,83,5-
322,Algebra II Honors,81,5-
420,Religion II- Christian Scriptures,90,5-
521,Chemistry Honors,78,5-
526,Chemistry Honors Lab,A,0.5-
621,American History Honors,87,5-
721,Sophomore Computers,89,1.5-
820,Phys Ed- Sophomore,A,1.5-
823,Sophomore Health,100,1.5-
130,English 3 CP,90,5-
214,Latin 3 Honors,81,5-
331,Pre-Calculus Honors,89,5-
430,Morality and Justice,87,5-
546,Physics CP,89,5-
550,Physics Lab,A,1-
631,American History Honors,91,5-
730,Computer Applications 3,89,2-
846,Phys Ed-AP Science Only,A,2-

这是我读取文件的代码。

public class readfromfile
{
public static void main(String[] args)
{
readfromfile rf = new readfromfile();
ArrayList<Student> Class = new ArrayList();
ArrayList<Integer> StudentAnswers = new ArrayList();



try
{
File ReportCard = new File("/home/*********/Desktop/Report_Card.txt");
Scanner NewScanner = new Scanner(ReportCard);

while(NewScanner.hasNextLine())
{
String line = NewScanner.nextLine();
int comma = line.indexOf(",");
int hyphen = line.indexOf("-");

String CourseNumber = line.substring(0,comma);
String CourseName = line.substring(comma,comma+1);
String FinalGrade = line.substring(comma+1, comma+2);
String Credit = line.substring(comma+2, hyphen);

ArrayList<Integer> CourseNumberArray = new ArrayList();
ArrayList<String> CourseNameArray = new ArrayList();
ArrayList<Integer> FinalGradeArray= new ArrayList();
ArrayList<Integer> CreditArray = new ArrayList();

Scanner searchline = new Scanner(CourseNumber);
Scanner searchline2 = new Scanner(CourseName);
Scanner searchline3 = new Scanner(FinalGrade);
Scanner searchline4 = new Scanner(Credit);

searchline.useDelimiter(",");
searchline2.useDelimiter(",");
searchline3.useDelimiter(",");
searchline4.useDelimiter(",");

for(int i = 0; i < 3; i++)
{
int temporary = searchline.nextInt();
CourseNumberArray.add(temporary);
}
for(int i = 0; i < 4; i ++)
{
String temporary = searchline2.next();
CourseNameArray.add(temporary);
}
for(int i = 0; i < 4; i++)
{
int temporary = searchline3.nextInt();
FinalGradeArray.add(temporary);

}
for(int i = 0; i < 4; i++)
{
int temporary = searchline4.nextInt();
CreditArray.add(temporary);
}

}



NewScanner.close();
}

catch(FileNotFoundException p)
{
System.out.println("File not found");
}

}
}

当我运行它时从控制台:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Student cannot be resolved to a type
Course cannot be resolved to a type
Course cannot be resolved to a type
CourseNumberArrayList cannot be resolved to a variable
CourseNameArrayList cannot be resolved to a variable
CreditArrayList cannot be resolved to a variable
Grades cannot be resolved to a type
Grades cannot be resolved to a type
CourseNumberArrayList cannot be resolved to a variable
FinalGradeArrayList cannot be resolved to a variable
CreditArrayList cannot be resolved to a variable

at readfromfile.main(readfromfile.java:14)

最佳答案

虽然你的帖子是missing a question ,看来你的问题是:

How can I fix java.lang.Error Unresolved compilation problem

第一个问题是<identifier> cannot be resolved to a type .

这意味着您需要 declare a class名为 <identifier> ,或更改它以匹配您已声明的类的名称。

第二个问题是<identifier> cannot be resolved to a variable .

这意味着您需要 declare a variable名为 <identifier> ,或更改它以匹配您已声明的变量的名称。

关于java - 将文本文件读入数组的字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28144637/

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