问题描述
大家好。
我需要一个算法,我可以从.csv文件中获取一个特殊学生的数据并将其写入另一个.txt文件那是空的。请注意,.csv文件是包含所有数据的文件,我需要将完整的学生从.csv文件移动到没有数据的.txt文件。让我来解释一下。以下是.csv文件
101,Helen Scott,2003,初学者,1,1,2,2,3,
102 ,James Jackson,2004,Amature,2,2,3,4,4,
103,Tim Moore,2005,新手,3,4,3,3,4,
$ b $ 104,Tom Smith,2004,Expert,4,5,3,5,4,
105,Jo Black,2004,Amature,4,3,2,2,1,
106,Mary Brown,2001,新手,4,4,3,3,4,
107,John Black,2006,初学者,1,1,1,1 ,2,
108,Mary Blue,2005,Amature,2,2,4,3,3
109,Jonney Depp,2007,Amature,3,3 ,4,2,2,
110,Mary Black,2005,新手,4,2,3,3,4,
现在我只需要了解一个特定学生的全部细节(姓名,身份证,年份等)。这里没有输入命令...
以下是3个文件,
1)主文件(包含的主文件) .csv和.txt)
2)XboxCompetetion文件(存储学生的所有属性)
3)和CompetitorList文件(这是我的地方)有读写文件代码,这是我想放置该算法的地方)。我会把相关的代码放在一起,以免造成混乱。
主文件
Hello Everyone.
Iam in need of an algorithm were i can fetch data of one particlar student from a .csv file and write it in another .txt file that is empty. Note that the .csv file is the one that has all the data and i need to move just once full student deatils from the .csv file to the .txt file that has no data in it. Let me explain furthure. below is a .csv file
101,Helen Scott,2003,Beginner,1,1,2,2,3,
102,James Jackson,2004,Amature, 2,2,3,3,4,
103,Tim Moore,2005,Novice, 3,4,3,3,4,
104,Tom Smith,2004,Expert, 4,5,3,5,4,
105,Jo Black,2004,Amature, 4,3,2,2,1,
106,Mary Brown,2001,Novice, 4,4,3,3,4,
107,John Black,2006,Beginner,1,1,1,1,2,
108,Mary Blue,2005,Amature, 2,2,4,3,3,
109,Jonney Depp,2007,Amature, 3,3,4,2,2,
110,Mary Black,2005,Novice, 4,2,3,3,4,
Now i just need to get full details of one particular student with everything(Name, id, year etc...). There is no input command needed here...
Below are 3 files,
1) the main file(The main file that contains both the .csv and .txt)
2) the XboxCompetetion file(That stores all the attributes of the student)
3) and the CompetitorList file(This is where i have the read and write file code and this is where i want to put that algorithm). I will just put the relevant codes so as not to make it confusing
The Main File
public class Main {
public static void main(String[] args) {
Manager sm = new Manager();
sm.run();
CompetitorList sl = new CompetitorList();
sl.readFile("XboxList.csv");
String report = sl.getTableOfXboxCompetetions();
sl.writeToFile("XboxResults.txt", report);
}
}
XboxCompetetion文件
The XboxCompetetion file
public class XboxCompetetion {
private int competitorNumber;
private Name competitorName;
private int year;
private String competetionLevel;
//private static final int[] NUM_MARKS = 3;
private int [] score;
public XboxCompetetion(int cNumber, Name cName, int year, String cLevel, int[] score)
{
competitorNumber = cNumber;
competitorName = cName;
this.year = year;
competetionLevel = cLevel;
//score = NUM_MARKS;
this.score = score;
}
CompetitorList文件
The CompetitorList file
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
//demonstrates using an ArrayList
public class CompetitorList {
//holds a list of XboxCompetetion objects
private ArrayList<XboxCompetetion> CompetitorList;
public XboxCompetetion Comp;
//create an empty arraylist
public CompetitorList() {
CompetitorList = new ArrayList<XboxCompetetion> ();
}
public void add(XboxCompetetion s) {
CompetitorList.add(s);
}
public void writeToFile(String filename, String report) {
FileWriter fw;
try {
fw = new FileWriter(filename);
fw.write("The report\n")
}
}
public void readFile(String filename) {
try {
File f = new File(filename);
Scanner scanner = new Scanner(f);
while (scanner.hasNextLine()) {
//read first line and process it
String inputLine = scanner.nextLine();
if (inputLine.length() != 0) {//ignored if blank line
processLine(inputLine);
}
}
}
//if the file is not found, stop with system exit
catch (FileNotFoundException fnf){
System.out.println( filename + " not found ");
System.exit(0);
}
}
private void processLine(String line) {
try {
String parts [] = line.split(",");
String id = parts[0];
int number = Integer.parseInt(id);
Name name = new Name(parts[1]);
String yearNum = parts[2];
yearNum = yearNum.trim(); //remove any spaces
int year = Integer.parseInt(yearNum);
String level = parts[3];
int scoreLength = parts.length - 4;
int scores[] = new int[scoreLength];
System.arraycopy(parts, 4, scores, 0, scoreLength);
//create Student object and add to the list
XboxCompetetion s = new XboxCompetetion(number, name, year, level, scores);
this.add(s);
}
}
感谢您提供的任何帮助。
Thank you for whatever help i can get.
推荐答案
101, Helen Scott, 2003, Beginner, 1, 1, 2, 2, 3,
102, James Jackson, 2004, Amature, 2, 2, 3, 3, 4,
103, Tim Moore, 2005, Novice, 3, 4, 3, 3, 4,
104, Tom Smith, 2004, Expert, 4, 5, 3, 5, 4,
105, Jo Black, 2004, Amature, 4, 3, 2, 2, 1,
似乎在第一列中有ID。 File-2?XboxCompetetion有相同的ID吗?
如果是这样的话,我只是想一般性地说,
1)你使用第一列将文件-2读入字典对象? ID作为键,作为数据的整行或其余部分 - 一个字符串或记录或结构
2)你打开文件-1并读取它循环播放
3)对于file-1中的每一行,您提取ID,然后按ID查找字典以获取存储的其他信息file-2
这就是你要问的 - 用普通钥匙连接两个文件?
'g'
seems to have ID's in the first column. Does File-2 ?XboxCompetetion have the same ID ?
If so, and Im only thinking in general terms,
1) you read file-2 into a dictionary object, using the 1st column ? ID as the key, and either the whole line or rest of the line as the data - one string or record or struct
2) you open file-1 and read it in a loop
3) for every line from file-1, you extract the ID, then lookup the dictionary by ID to get the other information that was stored from file-2
Is that what you're asking - 'joining two files together by a common key' ?
'g'
这篇关于我需要算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!