Results 1 to 4 of 4

Thread: โปรแกรมนับจำนวนคำจาก file (Word count)

  1. #1
    Administrator asylu3's Avatar
    Join Date
    Jun 2000
    Location
    Thailand
    Posts
    3,557


    โปรแกรมนับจำนวนคำจาก file (Word count)

    หมายเหตุสังเกตุบรรทัดที่ระบุว่า D://java_exam/test.txt ตรงนี้ให้เปลี่ยนตามตำแหน่ง file ของท่านเอง

    อธิบายโค้ด
    -มีการใช้ throws IOException เพราะต้องการลดความยุ่งยากของการใช้ try - catch
    -countln คือนับคำตัวสุดท้ายของแต่ละบรรทัด
    -count คือนับคำทั้งหมดแต่ละบรรทัดไม่รวมคำสุดท้าย
    โปรแกรมสามารถนับคำที่เว้นช่องว่างมากว่า 1 ช่องได้ทั้งด้านหน้าและด้านหลัง

    [src]
    import java.io.*;

    class WordCount
    {
    public static void main(String args[]) throws IOException
    {


    int countln=0,count=0;
    String out;
    FileInputStream fstream = new
    FileInputStream("D://java_exam/test.txt");
    DataInputStream in =new DataInputStream(fstream);

    while (in.available() !=0){
    out=in.readLine();
    char test[]=out.toCharArray();
    System.out.println (out);

    for(int c=0;c<test.length;c++){

    if(c<test.length-1 ){
    if( test[c]!=' ' && test[c+1]==' ' ){

    count++;


    }

    }
    }


    countln++;

    }

    in.close();
    System.out.println ("Total Word="+(count+ countln));
    }

    }

    [/src]

  2. #2
    Administrator asylu3's Avatar
    Join Date
    Jun 2000
    Location
    Thailand
    Posts
    3,557


    Re: โปรแกรมนับจำนวนคำจาก file

    version 2

    [src]
    import java.io.*;
    import java.util.*;


    public class Other_w_count {
    public static void main (String[] args) throws IOException {
    int count=0;
    String out;
    FileInputStream fstream = new FileInputStream("D://java_exam/test.txt");
    DataInputStream in =new DataInputStream(fstream);

    while (in.available() !=0){
    out=in.readLine();
    System.out.println(out);
    StringTokenizer st = new StringTokenizer(out, " ", false);

    while (st.hasMoreTokens()){
    count++;
    st.nextToken();
    }

    }
    System.out.println("Total word="+(count+1));
    }
    }[/src]

  3. #3
    Junior Member
    Join Date
    Sep 2002
    Location
    United States
    Posts
    9


    Re: โปรแกรมนับจำนวนคำจาก file

    อธิบายภาพหน่อยดิว่า
    โปรแกรมที่ว่าเป็นโปรแกรมที่ไม่ได้เรียกไฟล์จากที่อื่นใช่เป่า
    ถ้าใช่
    แสดงว่าโปรแกรมที่ต้องการจะต้องอ่าน input จากคำสั่งของ User เพราะฉะนั้นจะต้อง count จำนวนคำที่ user input เข้ามาใช่ป่าว
    ยิ่งอ่านยิ่ง งง อะ
    แบบว่าไม่เข้าใจ
    ??? ??? ???

  4. #4
    Administrator asylu3's Avatar
    Join Date
    Jun 2000
    Location
    Thailand
    Posts
    3,557


    Re: โปรแกรมนับจำนวนคำจาก file

    version 3 มี GUI



    [src]
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class Wcount2 extends JFrame{

    private JButton open;
    private JButton cal;
    private JLabel result;
    private JTextArea showtext;
    private JPanel bpanel;

    static int count;
    File testfilename;

    public Wcount2()
    {
    super("Word Calculation");

    open=new JButton("Open File");
    result=new JLabel("TOTAL WORD= ");
    cal=new JButton("Calculate");
    showtext=new JTextArea(100,100);
    Container c= getContentPane();
    c.setLayout(new BorderLayout());

    bpanel=new JPanel();
    bpanel.add(open);
    bpanel.add(cal);
    bpanel.add(result);

    //c.add(open,BorderLayout.NORTH);
    // c.add(cal,BorderLayout.AFTER_LINE_END);
    // c.add(result,BorderLayout.CENTER);

    c.add(showtext,BorderLayout.CENTER);
    c.add(bpanel,BorderLayout.SOUTH);

    cal.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent event) {
    showOut();
    }

    });

    open.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event) {
    openFile();
    }

    });

    setSize(400,200);
    show();
    }


    void showOut(){

    result.setText("TOTAL WORD= "+Integer.toString(count));
    }

    void openFile(){

    JFileChooser file=new JFileChooser();
    file.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int result=file.showOpenDialog(this);
    if (result==JFileChooser.CANCEL_OPTION)return;
    testfilename=file.getSelectedFile();


    System.out.println();
    try {
    calIt(testfilename);
    } catch (FileNotFoundException e) {

    e.printStackTrace();
    }
    }

    void calIt(File in_file) throws FileNotFoundException {
    count=0;//reset
    showtext.setText("");
    String out;
    FileInputStream fstream = null;

    fstream = new FileInputStream(in_file);



    DataInputStream some =new DataInputStream(fstream);
    System.out.println("Original Message:n");
    try {
    while (some.available() !=0){
    out=some.readLine();
    showtext.append(out+"n");
    //System.out.println(out);
    StringTokenizer st = new StringTokenizer(out, " ", false);

    while (st.hasMoreTokens()){
    count++;
    st.nextToken();
    }

    }
    fstream.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    //System.out.println("In calIt count:"+count);

    }

    public static void main (String[] args) {

    Wcount2 obj=new Wcount2();

    }
    }


    [/src]

Similar Threads

  1. ใครแก้ปัญหาได้หนอ pdf file หลังจากแปลงเป็น word
    By Yookung in forum มือใหม่สอบถามปัญหาการใช้งาน
    Replies: 0
    Last Post: 21-08-2009, 02:27 PM
  2. Replies: 0
    Last Post: 24-03-2009, 07:37 PM
  3. มาทำให้ Word 2003 อ่าน Word 2007 ได้ กันเถอะ ><"
    By crashctr in forum แนะนำ Software ต่างๆ
    Replies: 0
    Last Post: 02-02-2009, 05:07 PM
  4. MS. Word 2003 & MS. Word 2007 สำหรับพกพา
    By ong40600 in forum Window Application
    Replies: 0
    Last Post: 09-11-2008, 07:38 AM
  5. live it count
    By rabies in forum Non computer knowledge
    Replies: 0
    Last Post: 18-12-2006, 02:41 AM

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •