【发布时间】:2014-04-20 04:48:29
【问题描述】:
这是一个简单的家庭作业,过去几天一直让我发疯。如果我要使用几个数组,我可以在不久前完成它,但不得不使用 StringTokenizer 让我发疯。
我遇到的主要问题是读取 CSV 文件。我不知道该怎么做,以前的在线搜索只提出了超级激烈的解决方案,对于像我这样的初学者来说太过分了。
这是我的代码。如您所见,我不知道是使用.nextLine() 还是.NextToken()。两者似乎都不起作用。
对于那些想知道作业的人来说,基本上是读取前 4 个用逗号分隔的产品,然后读取其余行作为这 4 个产品的评分。基本上6行4列。第一行是产品,其余的是评分。
import java.util.StringTokenizer;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ProductRating {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner fileIn=null;
try{
fileIn = new Scanner(
new FileInputStream("C:/Users/Cristian/Desktop"));
}
catch (FileNotFoundException e)
{ // This block executed if the file is not found
// and then the program exits
System.out.println("File not found.");
System.exit(0);
}
//If Opened File Successful this runs
String products = "A";
String rantings ="0";
System.out.println("Gathering Ratings for Products");
do{
String delimiters = ", ";
StringTokenizer gatherProducts[]=new StringTokenizer[inputLine, delimeters];
gatherProducts=fileIn.nextLine();
}while(fileIn.hasNextLine()==true);
}
}
【问题讨论】:
-
+1 这是关于家庭作业的问题。
-
字符串标记器已被贬值,我很惊讶老师会要求您使用它。拆分是现在的事情。
-
小心,CSV 不仅仅是分隔符。你也有转义字符。
-
@Marichyasana 不鼓励使用
StringTokenizer,但不建议使用。 -
不管怎样,为什么会出现这个问题