What's new

Closed Basic series: string/data manipulation - 2

Status
Not open for further replies.

codyscott

Eternal Poster
Joined
Sep 13, 2017
Posts
388
Reaction
454
Points
279
Instead of reading from explicitly declared String, gawin natin read from a TEXT file.
(Pareho din ang laman ng text file. Make sure to save this text into a text file and name it "studentslist.txt"...no space, just one straight continuous line of text)

SAVE THIS AS studentslist.txt
Code:
Antonio|Dimaculangan|BS Nursing|3rd year|University of the Philippines##Jose|Hipolito|BS Civil Engineering|2nd Year|Adamson University##Maria|Capra|BS Accountancy|5th Year|University of Santo Tomas##Leo|Ramirez|Hotel and Restaurant Management|1st Year|STI College##Alan|Montelibano|Computer Science|4th Year|AMA


SAVE THIS AS MainApp2.java
Code:
/**
 * Part 2 of series
 * Reading it from file
 * Katulad din ng Series #1, but this time from a text file
 * Very minor change
 *
 * Author: Codyscott at phcorner.net
 *
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

public class MainApp2 {
 
    static File file = new File("studentslist.txt");
    static String longList = null;
    static BufferedReader reader = null;
    static ArrayList<String> studentRecords = new ArrayList<String>();
 
    public static void main(String[] args) {
        //STEPS 0: siguraduhin lang natin na file exist and if it does, gawa lang tayo ng longList
        //gaya ng series 1 (not necessarily pero just demonstration purposes)
        try {
            reader = new BufferedReader(new FileReader(file));
            String aVeryLongString;
            while((aVeryLongString=reader.readLine()) != null) {
                longList = aVeryLongString;
            }
        }catch(Exception e) {
            System.out.println("File not found.");
        }
     
        //STEP 1: display lang natin yung longList as is
        System.out.println(longList + "\n\n");
         
     
        //STEP 2: Let's split between ## to make records (or in database, rows)
        String[] studentRecords = longList.split("##");
     
     
        //STEP 3: Let's just verify
        //3a - bilangan kung ilang records
        System.out.println("TOTAL RECORDS: " + studentRecords.length + "\n\n");
     
        //3b - display natin isa isa yung records
        int count = 0;
        for(String aRecord : studentRecords) {
            count++;
            System.out.println(count + " : " + aRecord);
        }
     
        //********ok pagandahin pa natin ng konti
        //STEP 4: Let's split PER RECORD into fields
        //Let's loop inside the studentRecords
        int counter=0;
        for(String aRecord : studentRecords) {
         
            //split between "|"
            //take note: you need to escape using "\\|"
            String[] oneStudentArray = aRecord.split("\\|");
         
            System.out.println("\n" + ++counter);
            //step 4a: kada isang record, i-display natin
            for(String aField : oneStudentArray) {
                System.out.println(aField);
            }
         
        }
     
        /*at this point, we could have created a STUDENT OBJECT with this data
        and store it direct to a database.
        But for now, just know the basics
        */
     
    }
 
}
 
Status
Not open for further replies.
Back
Top