1

I am learning Uni student, so I need your help to solve this problems. I have following data in student.txt file mixture of UG_Student and GradStudent.

Jane Jones;111999;11 First St, Unitown,2999;0401 444 444;4483;4;6;7
Peter Smith;111998;22 Second St, West,5999;999 777;B Sc;2000;4672;5;5;5
Jim Smith;111988;25 Third St, South,6001;0412 222 333;BESE;1998;4672;4;5;5
John William;334344;123 Fig St, Townville,5655;0404 333 333;4333;5;6;5

First and Last line have a data in this order and denotes UG_Student:

Name;StudentID;Address; ContactNo;SubjectNo;Test1;Test2;Test3

Second and Third Line have a data in this order and denotes GradStudent:

Name;StudentID;Address; ContactNo;PreviousDegree;GradYr;SubjectNo;Test1;Test2;Test3

I have following code that can only read and print on JTextarea if txt file contains either UG_Student or GradStudent.

public void openFile()
    {
        try
        {
            input = new Scanner( new File("student.txt"));
            input.useDelimiter(";");
        }
        catch ( FileNotFoundException fileNotFoundException)
        {
            System.err.println("Error opening file.");
            System.exit(1);
        }
    }

    public String readFile()
    {
        Student ugStudent = new UGstudents();
        Student gradStudent = new Gradstudents();

        //int lineNumber = 1;

        //System.out.printf("running...");

        try
        {
            while ( input.hasNext() )
            {       
                String name = ugStudent.name = input.next();
                int studentID = ugStudent.studentID = input.nextInt();
                String address = ugStudent.address = input.next();
                int contactNo = ugStudent.contactNo = input.nextInt();
                int subjectNo = ugStudent.subjectNo = input.nextInt() ;
                int test1 = ugStudent.test1 = input.nextInt();
                int test2= ugStudent.test2 = input.nextInt();
                int test3= ugStudent.test3 = input.nextInt();
                String checkGrade= ugStudent.checkGrade();

                //System.out.println(name + studentID + address + contactNo + subjectNo +  test1 + test2 + test3 + checkGrade);
                resultColumn = ("Name\t Student Id\t Address\t\t Contact No\t Subject No\t Test 1\t Test 2\t Test 3\t Grade\n--------------------------------------------------------------------------------------------------------------------\n");
                result+=(name+ "\t" + studentID + "\t " + address + "\t " + contactNo + "\t " + subjectNo + " \t" +  test1 + "\t " + test2 + "\t " + test3 + "\t " + checkGrade);

            }               
        }
        catch ( NoSuchElementException elementException)
        {
            System.err.println( "File improperly formed." );
            input.close();
            System.exit( 1 );
        }
        catch ( IllegalStateException stateException )
        {
            System.err.println( "Error reading from file." );
            System.exit( 1 );
        }
        return resultColumn +result;
    }

    public void closeFile()

   {

      if ( input != null )

         input.close();

   } 
}

However, the given text file should contain mixture of data as shown above. My question is, how to read and print only either UG_Student or GradStudent data from that text file ? Sorry about long question but this is best I could.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Man Tamang
  • 49
  • 9

1 Answers1

0

It seems to me your best option is to use regular expressions. You could have expressions for each type of line and attempt to match each line, skipping those that don't apply. For example:

Pattern undergrad = Pattern.compile("(\\w*) (\\w*);(\\d*); ...");
Matcher matcher = undergrad.match(line);
if (matcher.matches()) {
    String firstName = matcher.group(1);
    String secondName = Matcher.group(2);
    String studentNumber = Matcher.group(3);
    ...
}

The advantage of using regular expressions is that you are matching the line and identifying the values to extract in a single instruction. See Pattern for more info on their use.

sprinter
  • 27,148
  • 6
  • 47
  • 78