Java 将文本文件中的双精度项添加到数组列表中
我有一个文本文件,它有数百个double,都用逗号分隔,我正在尝试编写一个方法,将每个double放入一个double数组列表中。这是我的密码:Java 将文本文件中的双精度项添加到数组列表中,java,Java,我有一个文本文件,它有数百个double,都用逗号分隔,我正在尝试编写一个方法,将每个double放入一个double数组列表中。这是我的密码: public void ReadFile(String inputfile) throws FileNotFoundException { File myFile = new File(inputfile); Scanner sc = new Scanner(myFile); try { while (sc.ha
public void ReadFile(String inputfile) throws FileNotFoundException {
File myFile = new File(inputfile);
Scanner sc = new Scanner(myFile);
try {
while (sc.hasNextDouble()) {
int i = 0;
arraylist.add(sc.useDelimiter(","));
i++;
}
} catch (Exception e) {
System.out.println("Error");
}
sc.close();
}
我遇到的问题是行arraylist.add(sc.usedimiter(“,”)
我得到一个错误,说“类型ArrayList中的add(Double)方法不适用于参数(Scanner)”。我不确定该怎么做才能解决这个问题。有什么帮助吗 必须将
useDelimiter
移动到循环外部。您还必须调用nextDouble
来迭代文件中的数字
public static List<Double> readFile(String inputfile) throws FileNotFoundException{
List<Double> arraylist = new ArrayList<Double>();
File myFile = new File(inputfile);
Scanner sc = new Scanner(myFile);
sc.useDelimiter(",");
try {
while (sc.hasNextDouble()) {
Double number = sc.nextDouble();
arraylist.add(number);
}
}catch (Exception e) {
System.out.println("Error");
}
sc.close();
return arraylist;
}
publicstaticlist读取文件(stringinputfile)抛出FileNotFoundException{
List arraylist=新建arraylist();
文件myFile=新文件(inputfile);
扫描仪sc=新扫描仪(myFile);
sc.useDelimiter(“,”);
试一试{
while(sc.hasNextDouble()){
双倍数字=sc.nextDouble();
arraylist.add(数字);
}
}捕获(例外e){
System.out.println(“错误”);
}
sc.close();
返回数组列表;
}
另外,请遵循Java中正确的命名约定。方法名称应以小写字母开头。必须将
useDelimiter
移动到循环外部。您还必须调用nextDouble
来迭代文件中的数字
public static List<Double> readFile(String inputfile) throws FileNotFoundException{
List<Double> arraylist = new ArrayList<Double>();
File myFile = new File(inputfile);
Scanner sc = new Scanner(myFile);
sc.useDelimiter(",");
try {
while (sc.hasNextDouble()) {
Double number = sc.nextDouble();
arraylist.add(number);
}
}catch (Exception e) {
System.out.println("Error");
}
sc.close();
return arraylist;
}
publicstaticlist读取文件(stringinputfile)抛出FileNotFoundException{
List arraylist=新建arraylist();
文件myFile=新文件(inputfile);
扫描仪sc=新扫描仪(myFile);
sc.useDelimiter(“,”);
试一试{
while(sc.hasNextDouble()){
双倍数字=sc.nextDouble();
arraylist.add(数字);
}
}捕获(例外e){
System.out.println(“错误”);
}
sc.close();
返回数组列表;
}
另外,请遵循Java中正确的命名约定。方法名称应以小写字母开头。正如@Hovercraft所说,您需要设置一次分隔符,然后将其上移到初始化扫描仪的位置。应该是这样的:
public void ReadFile(String inputfile) throws FileNotFoundException {
File myFile = new File(inputfile);
Scanner sc = new Scanner(myFile);
List<double> doublelist = new ArrayList<Double>();
//this is where you set the delimiter
sc.useDelimiter(",")
try {
while (sc.hasNextDouble()) {
doublelist.add(sc.nextDouble());
}
} catch (Exception e) {
System.out.println("Error");
}
sc.close();
}
public void ReadFile(字符串inputfile)引发FileNotFoundException{
文件myFile=新文件(inputfile);
扫描仪sc=新扫描仪(myFile);
List doublelist=新的ArrayList();
//这是设置分隔符的地方
sc.useDelimiter(“,”)
试一试{
while(sc.hasNextDouble()){
doublelist.add(sc.nextDouble());
}
}捕获(例外e){
System.out.println(“错误”);
}
sc.close();
}
正如@Hovercraft所说,您需要设置一次分隔符,然后将其移动到初始化扫描仪的位置。应该是这样的:
public void ReadFile(String inputfile) throws FileNotFoundException {
File myFile = new File(inputfile);
Scanner sc = new Scanner(myFile);
List<double> doublelist = new ArrayList<Double>();
//this is where you set the delimiter
sc.useDelimiter(",")
try {
while (sc.hasNextDouble()) {
doublelist.add(sc.nextDouble());
}
} catch (Exception e) {
System.out.println("Error");
}
sc.close();
}
public void ReadFile(字符串inputfile)引发FileNotFoundException{
文件myFile=新文件(inputfile);
扫描仪sc=新扫描仪(myFile);
List doublelist=新的ArrayList();
//这是设置分隔符的地方
sc.useDelimiter(“,”)
试一试{
while(sc.hasNextDouble()){
doublelist.add(sc.nextDouble());
}
}捕获(例外e){
System.out.println(“错误”);
}
sc.close();
}
这意味着它需要一个double
或者double
作为参数,但是您给了它一个扫描器
。请尝试sc.usedimiter(“,”).nextDouble()
或类似的方法。您需要重新读取扫描仪API,因为这不是您使用它的方式。设置分隔符一次,然后调用hasNextXxx()
/nextXxx()
方法对。这意味着它需要一个double
或double
作为参数,但您给了它一个Scanner
。请尝试sc.usedimiter(“,”).nextDouble()
或类似的方法。您需要重新读取扫描仪API,因为这不是您使用它的方式。设置一次分隔符,然后调用hasNextXxx()
/nextXxx()
方法对。