困扰system.arraycopy问题 [英] Confusing system.arraycopy problems

查看:100
本文介绍了困扰system.arraycopy问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我试图将数据从一个.csv文件复制到另一个.txt文件中。 csv文件是这样的



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,



我使用了一个处理方法,然后在readfile中调用处理方法......现在面临的问题是我可以将所有数据都输入到读取文件中,除了分数和程序向我显示我的处理文件有问题但错误细节也不是很详细... thi s是错误信息我得到了




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...

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...

推荐答案

您将字符串存储在
parts

中并将int存储在

scores

。如何将字符串复制到int?

. How can you copy string to int?

System.arraycopy(parts, 4, scores, 0, scoreLength);


这篇关于困扰system.arraycopy问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆