Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-读取文本文件并添加唯一ID_Java - Fatal编程技术网

Java-读取文本文件并添加唯一ID

Java-读取文本文件并添加唯一ID,java,Java,我正在努力学习Java,在我遇到的一个问题上,我真的很挣扎。我被要求写一个方法来读取一个文本文件,其中每一行代表一个对象的实例,例如SalesPerson 问题是要求我为读入的每一行添加一个标识符id,该id不在文本文件中。我在Sales Person类中使用构造函数、getter和setter方法声明了id,并使用我的方法在另一个类中读取下面的文本文件。然而,它不起作用,我不知道我会错在哪里。有人能给我一个指针吗。。?我将非常感激 public static Collection<Sal

我正在努力学习Java,在我遇到的一个问题上,我真的很挣扎。我被要求写一个方法来读取一个文本文件,其中每一行代表一个对象的实例,例如SalesPerson

问题是要求我为读入的每一行添加一个标识符id,该id不在文本文件中。我在Sales Person类中使用构造函数、getter和setter方法声明了id,并使用我的方法在另一个类中读取下面的文本文件。然而,它不起作用,我不知道我会错在哪里。有人能给我一个指针吗。。?我将非常感激

public static Collection<SalesPerson> readSalesData() {
    String pathname = CXU.FileChooser.getFilename();
    File aFile = new File(pathname);
    Scanner bufferedScanner = null;
    Set<SalesPerson> salesSet = new HashSet<>();

    try {
        int id;
        String name;
        String productCode;
        int sales;
        int years;
        Scanner lineScanner;
        String currentLine;
        bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));

        while(bufferedScanner.hasNextLine()) {
            currentLine = bufferedScanner.nextLine();
            lineScanner = new Scanner(currentLine);
            lineScanner.useDelimiter(",");
            id = salesPerson.getId();
            name = lineScanner.next(); //return the next token as a string
            years = lineScanner.nextInt();
            productCode = lineScanner.next(); // return the next token as a string
            sales = lineScanner.nextInt(); // return the next token as a double
            salesSet.add(new SalesPerson(id, name, years, productCode, sales));   
        }
    }
    catch (Exception anException) {
        System.out.println("Error: " + anException);
    }
    finally { 
        try {
            bufferedScanner.close();
        }
        catch (Exception anException) {
            System.out.println("Error: " + anException);
        }
    }
    return salesSet;
}


\\Constructor from Class SalesPerson
public SalesPerson(int aId, String aname, int aYears, String aProductCode, int aSales) {
    super(); // optional
    this.id = ++nextId;
    this.name = aname;
    this.years = aYears;
    this.productCode = aProductCode;
    this.sales = aSales;
}


公共静态集合readSalesData(){
String pathname=CXU.FileChooser.getFilename();
File aFile=新文件(路径名);
Scanner bufferedScanner=null;
Set salesSet=new HashSet();
试一试{
int-id;
字符串名;
字符串产品代码;
国际销售;
整数年;
扫描线扫描仪;
串电流线;
bufferedScanner=新扫描仪(新BufferedReader(新文件读取器(aFile));
while(bufferedScanner.hasNextLine()){
currentLine=bufferedScanner.nextLine();
lineScanner=新扫描仪(currentLine);
lineScanner.useDelimiter(“,”);
id=salesPerson.getId();
name=lineScanner.next();//以字符串形式返回下一个标记
年份=lineScanner.nextInt();
productCode=lineScanner.next();//以字符串形式返回下一个标记
sales=lineScanner.nextInt();//以双精度返回下一个令牌
添加(新销售人员(id、姓名、年份、产品代码、销售));
}
}
捕获(异常和异常){
System.out.println(“错误:+anException”);
}
最后{
试一试{
bufferedScanner.close();
}
捕获(异常和异常){
System.out.println(“错误:+anException”);
}
}
返回销售集合;
}
\\来自类SalesPerson的构造函数
公共销售人员(int-aId、String-aname、int-aYears、String-aProductCode、int-aSales){
super();//可选
this.id=++nextId;
this.name=aname;
这.年=年;
this.productCode=aProductCode;
这个。销售=aSales;
}

请检查以下代码,我试图让事情变得简单一点:

package.com.project;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.util.Collection;
导入java.util.HashSet;
导入java.util.Set;
公共类临时工{
公共静态void main(字符串[]args){
Set salersons=(Set)readSalesData();
System.out.println(salersons.toString());
}
公共静态集合readSalesData(){
Set salersons=new HashSet();
试一试{
File File=新文件(“D:/File.txt”);
FileReader FileReader=新的FileReader(文件);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
弦线;
而((line=bufferedReader.readLine())!=null){
if(line.isEmpty())
打破
String[]rowData=line.split(“;”);
添加(新销售人员(行数据[0].trim(),整数.parseInt(行数据[1].trim()),行数据[2].trim(),整数.parseInt(行数据[3].trim()));
}
fileReader.close();
}捕获(例外情况除外){
系统输出打印项次(ex);
}
退货销售人员;
}
}
包com.project;
公开课销售员{
//Static为每个新实例保留保留值
私有静态int AUTO_ID=1;
私有int-id;
私有字符串名称;
私人年资;
私有字符串代码;
私人销售;
公共销售人员(){
}
公共销售人员(字符串名称、整数年、字符串产品代码、整数销售){
this.id=AUTO_id;
this.name=名称;
本年=年;
this.productCode=productCode;
这个。销售=销售;
自动识别++;
}
//接球手和接球手。。。
@凌驾
公共字符串toString(){
返回“ID:+ID+”,名称:“+Name+”,年份:“+Years+”,产品代码:“+productCode+”,销售:“+Sales+System.lineSeparator()”;
}
}
这是我的数据文件:

FullName1 ; 20; p-code-001; 10
FullName2 ; 30; p-code-002; 14
FullName3 ; 18; p-code-012; 1040

在第
id=salesson.getId()行中,您在哪里声明了
salesson
另外,您能否具体说明哪些不起作用?如果无法编译,错误是什么。如果它编译但崩溃,错误消息是什么?如果它没有崩溃但产生了错误的输出,那么输出是什么?提供这些将帮助我们帮助您。祝你好运在while循环中保留一个计数器。将其初始化为0,并对从文件中读取的每一行递增1。将计数器用作对象的唯一id<代码>…新销售人员(++计数器、姓名、年份、产品代码、销售))fram-抱歉,我的版本控制还有很多需要改进的地方-因此我无法重播错误消息。harsh-我没有声明它:-(嘿,侯赛因-非常感谢!你的方法很有效-但是我有一个小问题。我的销售人员构造函数只能接受一个表示名称的字符串参数。因此我不知道如何重新构建此构造函数以获得相同的输出。嘿@Barca1878,不客气。请提供更多信息??我没有得到你要的输出转到.Hi Hussein,请忽略我的最后一个问题。如果我使用此命令集合调用方法bob smith=temp.readSalesData();-则这似乎有效-但是我需要使用temp bob smith=new temp()调用该方法;temp.readSalesData。你知道为什么不能这样工作吗?这似乎是无效的java声明
temp bob smith=new temp();
你介意澄清一下吗?如果你愿意,你可以DM…请你把我的答案标记为已批准