Jump to content
Sign in to follow this  
ki_bot

Aturcara Java

Recommended Posts

Minta tolong pada yg tahu, macam mana nak buat satu aturcara java yg boleh baca dari satu fail dan data2 dr fail tersebut disimpan pada pembolehubah untuk di kira dan disimpan pada fail yg lain… nih yg saya cuba buat tapi xjadi, kat bahagian kira sej tu xda respon jer..

import java.io.*;
import java.util.*;

public class Jadual_try
{

    public static void main (String[] args) throws IOException
    {
        String[][] simpan = new String[3][4];
    
        BufferedReader input = new BufferedReader(new FileReader ("pelajar_try.txt"));
            
             String baris;
        String baru = "saya";
        int i = 0;
        int j = 0;

        while ((baris = input.readLine()) != null)
        {            
            StringTokenizer tokens = new StringTokenizer (baris);
            while (tokens.hasMoreTokens())
            {
                
                simpan[i][j] = tokens.nextToken();
                System.out.println(simpan[i][j]);
                j++;            
            }
            
            i++;
            j=0;
        } 

        
        input.close();

        PrintWriter output = new PrintWriter(new FileWriter ("pelajarOut.txt"));

        int sej = 0;
        for (int n=0; n<simpan.length; n++)
        {
            for (int m = 0; m<simpan[n].length; m++)
            {
                if (simpan[n][m] == "SEJ")
                    sej += 1;
                else 
                    output.println(simpan[n][m]);
            }
        }
                
        output.println("SEJ = " +sej);    

        

        output.close();
    }

}

data dari fail input

B001 SEJ DIT 3

B003 BM DIT 3

B009 SEJ DPP 2

data fail output

B001

SEJ

DIT

3

B003

BM

DIT

3

B009

SEJ

DPP

2

SEJ = 0

Share this post


Link to post
Share on other sites

1st, declare dulu outputstream & inputstream

2nd, masa readline tu, trus buat processing (nak save ke kira2 ke ape ke)

3rd, trus save output ke file

kiranya, takde la ko nak buat loop sampai 2kali, sekali ckup

one more, ko output kan dulu ke command prompt, da ok baru save etc..

p/s : lagi molek kalo ko buat research (google) dulu..

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...