我是个编程新手,需要别人帮忙来完成我的任务。在.txt文件中有信息,它被分为5列,并有几行。它看起来像这样:
| | | | | |
| --------------|--------------|--------------|--------------|--------------|
| 橡胶|二|0.3|三点零八分|4016920000|
| 笔|三|0.3|五点零四|9608101000|
| 笔|二|0.2|二、五|9608101000|
| 铅笔|五|0.24|六、三|9609101000|
| 铅笔|三|0.18|四、八|9609101000|
第一通信是描述,第二通信是数量,第三通信是重量,第四通信是价格,第五通信是关税代码。列由空格分隔。我必须根据关税代码重新排列这些行,换句话说,我必须为每个关税代码计算数量,重量和价格。
对于这个例子,结果必须是这样的:
| | | | | |
| --------------|--------------|--------------|--------------|--------------|
| 橡胶|二|0.3|三点零八分|4016920000|
| 笔|五|0.5|七五四|9608101000|
| 铅笔|八个|0.42|11.1|9609101000|
对于我的观点,我需要创建二维数组,填充这些数据,并使计算正确的行,但我不知道如何做到。下面是我现在的代码。我将不胜感激。先谢谢你了。
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.util.Scanner;
import java.util.Iterator;
import java.util.ArrayList;
import java.io.FileWriter;
import java.util.Arrays;
import java.io.*;
public class Mail {
public static void main(String args[]) throws Exception{
File file1 = new File("C: \"Test for java input.txt\");
if (file1.length() == 0) { System.out.println("File is empty"); System.exit(0);}
FileInputStream fis = new FileInputStream(file1);
byte[] byteArray = new byte[(int)file1.length()];
fis.read(byteArray);
String data = new String(byteArray);
String[] stringArray = data.split("\r\n");
System.out.println("Number of lines in the file are ::"+stringArray.length);
fis.close();
BufferedReader br = new BufferedReader (new FileReader ( "C: Test for java input.txt"));
String line;
char[][] maze = new char[stringArray.length][5];
while ((line = br.readLine() ) != null ){
char[] row = line.toCharArray();
int x = 0;
for (int i = 0; i < row.length; i++) {
maze[x][i] = row[i];
System.out.print(maze[i]);
x++;
}
System.out.printf("%n"); //mention this
}
File file2 = new File("C: "Test for java output.txt");
int text = stringArray.length;
try (PrintWriter out = new PrintWriter(file2, StandardCharsets.UTF_8))
{
out.print(text);
System.out.println("Successfully written data to the file");
} catch (IOException e) {
e.printStackTrace();
}
}
}
我试过了,但这是错误的。
BufferedReader br = new BufferedReader (new FileReader ( "C:Test for java input.txt"));
String line;
char[][] maze = new char[stringArray.length][5];
while ((line = br.readLine() ) != null ){
char[] row = line.toCharArray();
int x = 0;
for (int i = 0; i < row.length; i++) {
maze[x][i] = row[i];
System.out.print(maze[i]);
x++;
}
System.out.printf("%n"); //mention this
}
1条答案
按热度按时间h5qlskok1#
这将是伟大的,如果你能张贴的文本文件快照,究竟你需要什么