1

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)
jmj
  • 225,392
  • 41
  • 383
  • 426
WonderphuL
  • 49
  • 7
  • Note : Use `Scanner` if you have to *parse* a file.. If you just wanna read it line by line, use `FileReader + BufferedReader`.. It will be more efficient. Also, if you close the `Scanner`, close the finally block with separate try-catch block – TheLostMind Jan 26 '15 at 04:03
  • Sorry, I am very new to coding and have never used a bufferedreader. Do you have an example you could link? – WonderphuL Jan 26 '15 at 04:06
  • [Related question](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file) – TheLostMind Jan 26 '15 at 04:08
  • Thank you TheLostMind :) – WonderphuL Jan 26 '15 at 04:08
  • You are welcome :).. Look at *Knubo's* answer to that question – TheLostMind Jan 26 '15 at 04:10
  • I tried using this code: try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } String everything = sb.toString(); } and it doesnt seem to work still – WonderphuL Jan 26 '15 at 04:25
  • Did you give the exact file path and name?.. What is the error? – TheLostMind Jan 26 '15 at 04:27
  • Downvoted this because the question will not help the community. If everyone starts posting their compilation errors on stackoverflow, it would soon become a "resolve my compilation error" website. – CKing Jan 26 '15 at 06:29

1 Answers1

0

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.

Community
  • 1
  • 1
gknicker
  • 5,321
  • 2
  • 19
  • 37