Diễn đàn giao lưu
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.
Tìm kiếm
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» Khai giảng lớp luyện thi N2 và N3 tại Trung tâm Nhật Ngữ Top Globis
Format String EmptyTue Feb 07, 2012 1:53 pm by onlink

» Vietpon! Mua sản phẩm chất lượng, giá tốt.
Format String EmptyWed Dec 07, 2011 1:53 pm by onlink

» Học tiếng Nhật - Top Globis
Format String EmptyWed Dec 07, 2011 1:44 pm by onlink

» Học tiếng Nhật - Top Globis
Format String EmptyWed Dec 07, 2011 1:32 pm by onlink

» Học tiếng Nhật - Top Globis
Format String EmptyWed Sep 21, 2011 2:21 pm by onlink

» Học tiếng Nhật - Top Globis
Format String EmptyWed Aug 10, 2011 2:25 pm by onlink

» Khai giảng lớp đàm thoại sơ trung cấp tại Top Globis
Format String EmptyWed Jun 15, 2011 11:24 am by onlink

» Tiếng Nhật online xu thế mới của thời đại- Top Globis
Format String EmptyWed Jun 15, 2011 11:22 am by onlink

» PHẢN XẠ NGẪU NHIÊN LIÊN TỤC-p2 Học tiếng Nhật mới
Format String EmptyTue Mar 08, 2011 5:51 pm by onlink


Format String

2 posters

Go down

Format String Empty Format String

Bài gửi by baotrung Mon May 10, 2010 8:57 pm

Code:

        StringBuilder sb = new StringBuilder();
        Formatter formatter = new Formatter(sb);
        formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d");
        System.out.println(sb);

Kết quả sẽ là: d c b a
baotrung
baotrung
Trưởng Lão

Tổng số bài gửi : 225
Reputation : 4
Join date : 13/01/2010
Age : 34

Về Đầu Trang Go down

Format String Empty Re: Format String

Bài gửi by baotrung Mon May 10, 2010 9:00 pm

Ngoài ra bạn còn có thể dùng cách sau:
Code:

              String s = String.format("Welcome %s at %s", "Real's HowTo", "http://giaoluu.forum-viet.net");
        System.out.println(s);

Kết quả sẽ là: Welcome Real's HowTo at https://giaoluu.forum-viet.net
baotrung
baotrung
Trưởng Lão

Tổng số bài gửi : 225
Reputation : 4
Join date : 13/01/2010
Age : 34

Về Đầu Trang Go down

Format String Empty Re: Format String

Bài gửi by tigker Tue May 11, 2010 8:00 pm

Mình xem thực sự không hiểu nó giúp gì cho bài toán của mình, hay là bạn xem lại hộ minh đi được không. Very Happy

tigker
Thành viên có đóng góp

Tổng số bài gửi : 3
Reputation : 0
Join date : 11/05/2010

Về Đầu Trang Go down

Format String Empty Re: Format String

Bài gửi by baotrung Tue May 11, 2010 8:16 pm

Ủa bài toán của bạn gặp lỗi hả. Bạn thử post bài của bạn lên mình xem giúp cho
baotrung
baotrung
Trưởng Lão

Tổng số bài gửi : 225
Reputation : 4
Join date : 13/01/2010
Age : 34

Về Đầu Trang Go down

Format String Empty Re: Format String

Bài gửi by tigker Tue May 11, 2010 8:23 pm

Mình là hai62bk@gmail.com ấy mà. Mình send bài cho bạn rồi mà. Thui post tiếp cho tiện theo dõi Very Happy. Mà nó không phải là lỗi, mình chả biết đạt sao nữa, bạn cứ run đi nhá Very Happy. Save file thì ok còn lúc load ấy.

Class Student :

Code:


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Tigker
 */
public class Student {
    String tenlop;
    String mahocvien = "";
    String hoten = "";
    int tuoi = 0;
    double diem = 0;

    Student() {

    }

    public double getDiem(){
        return this.diem;
    }

    Student(String tenlop, String _mahocvien, String _hoten, int _tuoi, double _diem){
        this.tenlop = tenlop;
        this.mahocvien = _mahocvien;
        this.hoten = _hoten;
        this.tuoi = _tuoi;
        this.diem = _diem;
    }

    public String toString(){
        return tenlop + " " + mahocvien + " " + hoten + " " + tuoi + " " + diem;
    }
}

Class StudentList :

Code:
import java.io.*;
import java.util.*;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Tigker
 */
public class StudentList {

    LinkedList hs = new LinkedList();

    StudentList() {
    }
    Scanner scan = new Scanner(System.in);

    public void addObj(Student s){
        hs.add(s);
    }

    public void addStudent() {
        String tenlop = "";
        String mahocvien = "";
        String hoten = "";
        int tuoi = 0;
        double diem = 0.0;

        while (true) {
            System.out.println("Nhap lop sinh vien : ");
            tenlop = scan.nextLine();

            System.out.println("Nhap ma sinh vien : ");
            mahocvien = scan.nextLine();

            System.out.println("Nhap ho ten sinh vien : ");
            hoten = scan.nextLine();

            System.out.println("Nhap tuoi sinh vien : ");
            tuoi = scan.nextInt();
            scan.nextLine();

            System.out.println("Nhap diem hoc sinh : ");
            diem = scan.nextDouble();
            scan.nextLine();

            Student s = new Student(tenlop, mahocvien, hoten, tuoi, diem);

            hs.add(s);

            System.out.println("Ban co muon nhap tiep khong : ");
            String choice = scan.nextLine();

            if (choice.equalsIgnoreCase("N")) {
                break;
            } else {
                continue;
            }
        }

        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new FileOutputStream("test.txt"));
            dos.write(hs.size());
            for (Student s : hs) {
                dos.writeUTF(s.tenlop);
                dos.writeUTF(s.mahocvien);
                dos.writeUTF(s.hoten);
                dos.writeInt(s.tuoi);
                dos.writeDouble(s.diem);
            }
        } catch (Exception e) {
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (Exception e) {
            }
        }

    }

    public LinkedList loadToFile() {
        LinkedList l = new LinkedList();

        try {
            DataInputStream dis = new DataInputStream(new FileInputStream("test.txt"));

            int count = dis.readInt();

            for (int i = 0; i < count; i++) {
                String tenlop = dis.readUTF();
                String mahocvien = dis.readUTF();
                String hoten = dis.readUTF();
                int tuoi = dis.readInt();
                double diem = dis.readDouble();

                Student s = new Student(tenlop, mahocvien, hoten, tuoi, diem);

                l.add(s);
            }
            dis.close();
        } catch (IOException ioe) {
        }
        return l;
    }

Main :

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.*;

/**
 *
 * @author Tigker
 */
public class StudentManage {

    public static void main(String[] args) {
        StudentList sh = new StudentList();

//        Student[] s = new Student[4];
//        s[0] = new Student("gdsfg","sadf","adsf",32,4);
//        s[1] = new Student("gdsfg","sadf","adsf",32,4);
//        s[2] = new Student("gdsfg","sadf","adsf",32,4);
//        s[3] = new Student("gdsfg","sadf","adsf",32,4);
//
//        NewClass nc = new NewClass();
//
//        nc.saveToFile(s);
//
//        Student[] in = nc.loadFromFile();
//
//        for(Student s1 : in){
//            System.out.println(s1);
//        }

        LinkedList l = sh.loadToFile();

        for(Student s : l){
            Student stu = new Student(s.tenlop,s.mahocvien,s.hoten,s.tuoi,s.diem);
            sh.addObj(stu);
            System.out.println(stu);
        }

tigker
Thành viên có đóng góp

Tổng số bài gửi : 3
Reputation : 0
Join date : 11/05/2010

Về Đầu Trang Go down

Format String Empty Re: Format String

Bài gửi by baotrung Tue May 11, 2010 8:32 pm

Phương thức addStudent của bạn đã bị sai. Thay vì dùng dos.writeInt(hs.size()); bạn lại dùng dos.write(hs.size());. Bên dưới là phương thức mình đã sữa lỗi bạn có thể tham khảo.

Code:

public void addStudent() {
        String tenlop = "";
        String mahocvien = "";
        String hoten = "";
        int tuoi = 0;
        double diem = 0.0;

        while (true) {
            System.out.println("Nhap lop sinh vien : ");
            tenlop = scan.nextLine();

            System.out.println("Nhap ma sinh vien : ");
            mahocvien = scan.nextLine();

            System.out.println("Nhap ho ten sinh vien : ");
            hoten = scan.nextLine();

            System.out.println("Nhap tuoi sinh vien : ");
            tuoi = scan.nextInt();
            scan.nextLine();

            System.out.println("Nhap diem hoc sinh : ");
            diem = scan.nextDouble();
            scan.nextLine();

            Student s = new Student(tenlop, mahocvien, hoten, tuoi, diem);

            hs.add(s);

            System.out.println("Ban co muon nhap tiep khong : ");
            String choice = scan.nextLine();

            if (choice.equalsIgnoreCase("N")) {
                break;
            } else {
                continue;
            }
        }

        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new FileOutputStream("sinhvien.dat"));
            dos.writeInt(hs.size());
            for (Student s : hs) {
                dos.writeUTF(s.tenlop);
                dos.writeUTF(s.mahocvien);
                dos.writeUTF(s.hoten);
                dos.writeInt(s.tuoi);
                dos.writeDouble(s.diem);
            }
        } catch (Exception e) {
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (Exception e) {

            }
        }

    }
baotrung
baotrung
Trưởng Lão

Tổng số bài gửi : 225
Reputation : 4
Join date : 13/01/2010
Age : 34

Về Đầu Trang Go down

Format String Empty Re: Format String

Bài gửi by tigker Tue May 11, 2010 9:22 pm

Ác thế, ông nhớ lần tr bảo tôi sửa tôi đã sửa rồi mà, sao giờ vẫn thế, hay quên chưa save lại file rồi. Làm mất 1 ngày oan cứ đoán già đoán non là do trôi dòng tao kí tự lạ trong file.

tigker
Thành viên có đóng góp

Tổng số bài gửi : 3
Reputation : 0
Join date : 11/05/2010

Về Đầu Trang Go down

Format String Empty Re: Format String

Bài gửi by Sponsored content


Sponsored content


Về Đầu Trang Go down

Về Đầu Trang


 
Permissions in this forum:
Bạn không có quyền trả lời bài viết