Tìm kiếm
Latest topics
Format String
2 posters
Trang 1 trong tổng số 1 trang
Format String
- 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- Trưởng Lão
- Tổng số bài gửi : 225
Reputation : 4
Join date : 13/01/2010
Age : 34
Re: Format String
Ngoài ra bạn còn có thể dùng cách sau:
Kết quả sẽ là: Welcome Real's HowTo at https://giaoluu.forum-viet.net
- 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- Trưởng Lão
- Tổng số bài gửi : 225
Reputation : 4
Join date : 13/01/2010
Age : 34
Re: Format String
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.
tigker- Thành viên có đóng góp
- Tổng số bài gửi : 3
Reputation : 0
Join date : 11/05/2010
Re: Format String
Ủ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- Trưởng Lão
- Tổng số bài gửi : 225
Reputation : 4
Join date : 13/01/2010
Age : 34
Re: Format String
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 . Mà nó không phải là lỗi, mình chả biết đạt sao nữa, bạn cứ run đi nhá . Save file thì ok còn lúc load ấy.
Class Student :
Class StudentList :
Main :
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
Re: Format String
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- Trưởng Lão
- Tổng số bài gửi : 225
Reputation : 4
Join date : 13/01/2010
Age : 34
Re: Format String
Á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
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|
Tue Feb 07, 2012 1:53 pm by onlink
» Vietpon! Mua sản phẩm chất lượng, giá tốt.
Wed Dec 07, 2011 1:53 pm by onlink
» Học tiếng Nhật - Top Globis
Wed Dec 07, 2011 1:44 pm by onlink
» Học tiếng Nhật - Top Globis
Wed Dec 07, 2011 1:32 pm by onlink
» Học tiếng Nhật - Top Globis
Wed Sep 21, 2011 2:21 pm by onlink
» Học tiếng Nhật - Top Globis
Wed Aug 10, 2011 2:25 pm by onlink
» Khai giảng lớp đàm thoại sơ trung cấp tại Top Globis
Wed Jun 15, 2011 11:24 am by onlink
» Tiếng Nhật online xu thế mới của thời đại- Top Globis
Wed 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
Tue Mar 08, 2011 5:51 pm by onlink