What's new

Closed Basic series: string/data manipulation - 1

Status
Not open for further replies.

codyscott

Eternal Poster
Joined
Sep 13, 2017
Posts
388
Reaction
454
Points
279
Mga 'tol, very basic data/string manipulation.
Paano magbasa ng data (String) and manipulate.
I will slowly illustrate how to read and manipulate String....and then create OBJECT out of it....to be used for database.

For now, here's a very basic code na kaya nyong pag-aralan.

SAVE THIS AS MainApp.java
Code:
/**
 * Super basic pero powerful itong concept na ito
 * Laging ginagamit ang procedure na ito sa paggawa ng application
 * Lalo na sa database or kaya naman sa data mining
 * This is purely array muna. Hindi tatayo gagamit muna ng objects
 *
 * Author: Codyscott at phcorner.net
 *
 */

public class MainApp {
 
    static String longList = "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";
 
    public static void main(String[] args) {
        //STEP 0: display lang natin yung longList as is
        System.out.println(longList + "\n\n");
   
        //STEP 1: recognize the dividers
        // "##" divides PER record (o kaya per student)
        // "|" divides PER field (o kaya student data)
   
        //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
        */
   
    }
 
}
 
Last edited:
Status
Not open for further replies.
Back
Top