Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
	}
}


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;
	}


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.
Posted
Updated 2-Nov-13 15:22pm
v2

1 solution

Not sure I understand your problem fully, but

you have a file with some data - file-1
you have another file with some data - file-2
you have to write a combined file, such that for every line/record in file-1, you add some data from file-2

yes ?

the obvious issue, is, there must be some piece of information common to the two files .. your file :-

VB
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,


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'
 
Share this answer
 
Comments
Maazatron 2-Nov-13 21:24pm    
Thank you for your answer. However i have update the question to make it more clear.. heres the thing..
1) All the data containg atleast 10 students is in the .csv file (According to you file 1)
2) There is no data in the .txt file (According to you file 2)
3) I want to move one of the students full details from the .csv (file 1) file into the other .txt file (file 2)

Regards...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900