Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone

Iam trying to copy data from one .csv file to another .txt file in java. The csv file is like this

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,

Iam using a processingline method for that after which the processing method is called in the readfile... now the problem iam facing with is that i can get all the data into the read file except the scores and the program is indicating to me that there is a problem with my processing file but the error detail is also not very detailed... this is the error msg iam getting

Exception in thread "main" java.lang.ArrayStoreException
	at java.lang.System.arraycopy(Native Method)
	at CompetitorList.processLine(CompetitorList.java:288)
	at CompetitorList.readFile(CompetitorList.java:255)
	at Main.main(Main.java:7)

so heres the code for the procesing file...

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

    //for these two formatting errors, ignore lines in error and try and carry on
    
    //this catches trying to convert a String to an integer
    catch (NumberFormatException nfe) {
    	String error = "Number conversion error in '" + line + "'  - " 
    	                  + nfe.getMessage();
    	System.out.println(error);
    }
    //this catches missing items if only one or two items
    //other omissions will result in other errors
    catch (ArrayIndexOutOfBoundsException air) {
    	String error = "Not enough items in  : '" + line
    	                        + "' index position : " + air.getMessage();
    	System.out.println(error);
    }
    
}


Can someone please tell me where iam going wrong...
Posted
Updated 3-Nov-13 22:31pm
v2

1 solution

you stored string in
parts
and stored int in
scores
. How can you copy string to int?
System.arraycopy(parts, 4, scores, 0, scoreLength);
 
Share this answer
 

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