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

时间:2015-01-26 04:01:55

标签: java arrays file-io arraylist java.util.scanner

我正在尝试将此文件读入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)

1 个答案:

答案 0 :(得分:0)

虽然您的帖子是missing a question,但似乎您的问题是:

  

如何修复java.lang.Error未解决的编译问题

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

这意味着您需要declare a class名称为<identifier>,或者更改它以匹配您已宣布的班级名称。

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

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