简体   繁体   中英

Reading a text file into a string for an array

I am trying to read this file into Java. I need to calculate GPA, adjusted and unadjusted. Here is the file:

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- 

This is my code to read the file.

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"); 
        }

    }
}

From the console when I run it:

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)

Though your post is missing a question , it seems like your question is:

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

The first problem is <identifier> cannot be resolved to a type .

This means you need to declare a class with the name <identifier> , or change it to match the name of a class you've already declared.

The second problem is <identifier> cannot be resolved to a variable .

This means you need to declare a variable with the name <identifier> , or change it to match the name of a variable you've already declared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM