Đây là một bài tập khác hay về mảng, ở đây bạn phải tìm cách để chuyển đổi nột dung của một tập tin văn bản dạng cột thành các mảng trong Java.
Bài tập - Exercises
Chuyển nội dụng tập tin data.txt có 3 cột INT01, INT02, INT03 có nội dung như bên dưới thành 3 mảng int khác nhau và in nội dung nó ra màn hình.
data.txt
INT01 INT02 INT03
100 65 33
77 78 98
100 89 44
89 66 86
91 75 85
95 78 68
84 82 44
92 93 100
100 65 33
77 78 98
100 89 44
89 66 86
91 75 85
95 78 68
84 82 44
92 93 100
Mã nguồn - Source Code
/**
* @(#)ConvertColToArray.java
*
* ConvertColToArray application
*
* @author Bui Ngoc Son
* @website vnlives.net
* @version 1.00 2014/6/21
*/
import java.io.*;
import java.util.*;
public class ConvertColToArray {
public static void main(String[] args) throws IOException {
String[] columns = new String[3];
Scanner input;
String str;
StringTokenizer strTokens;
int lines = 0;
input = new Scanner(new File("D:\\data.txt"));
while (input.hasNext()) {
str = input.nextLine();
lines = lines + 1;
}
input.close();
int[] row1 = new int[lines - 1];
int[] row2 = new int[lines - 1];
int[] row3 = new int[lines - 1];
input = new Scanner(new File("D:\\data.txt"));
str = input.nextLine();
strTokens = new StringTokenizer(str);
columns[0] = strTokens.nextToken();
columns[1] = strTokens.nextToken();
columns[2] = strTokens.nextToken();
int count = 0;
while (input.hasNext()) {
str = input.nextLine();
strTokens = new StringTokenizer(str);
row1[count] = Integer.parseInt(strTokens.nextToken());
row2[count] = Integer.parseInt(strTokens.nextToken());
row3[count] = Integer.parseInt(strTokens.nextToken());
count = count + 1;
}
System.out.println(columns[0] + ":" + Arrays.toString(row1));
System.out.println(columns[1] + ":" + Arrays.toString(row2));
System.out.println(columns[2] + ":" + Arrays.toString(row3));
}
}
* @(#)ConvertColToArray.java
*
* ConvertColToArray application
*
* @author Bui Ngoc Son
* @website vnlives.net
* @version 1.00 2014/6/21
*/
import java.io.*;
import java.util.*;
public class ConvertColToArray {
public static void main(String[] args) throws IOException {
String[] columns = new String[3];
Scanner input;
String str;
StringTokenizer strTokens;
int lines = 0;
input = new Scanner(new File("D:\\data.txt"));
while (input.hasNext()) {
str = input.nextLine();
lines = lines + 1;
}
input.close();
int[] row1 = new int[lines - 1];
int[] row2 = new int[lines - 1];
int[] row3 = new int[lines - 1];
input = new Scanner(new File("D:\\data.txt"));
str = input.nextLine();
strTokens = new StringTokenizer(str);
columns[0] = strTokens.nextToken();
columns[1] = strTokens.nextToken();
columns[2] = strTokens.nextToken();
int count = 0;
while (input.hasNext()) {
str = input.nextLine();
strTokens = new StringTokenizer(str);
row1[count] = Integer.parseInt(strTokens.nextToken());
row2[count] = Integer.parseInt(strTokens.nextToken());
row3[count] = Integer.parseInt(strTokens.nextToken());
count = count + 1;
}
System.out.println(columns[0] + ":" + Arrays.toString(row1));
System.out.println(columns[1] + ":" + Arrays.toString(row2));
System.out.println(columns[2] + ":" + Arrays.toString(row3));
}
}
Kết quả - Result
No comments:
Post a Comment