Java 程序只读取文件中的第一行

Java 程序只读取文件中的第一行,java,Java,我试图读取一个文件,其中一行包含一个名称,第二行后跟数字。第三行是名字,第四行是数字,依此类推 我用main方法测试了它。例如,我的文件包含名称“Bobby”,他的telno是123456,当我运行lookUpEntry(“Bobby”)时,我应该将他的telno返回给我。如果文件的第一个名字是“Bobby”,则此选项有效。如果不是第一个,则程序似乎无法识别名称并返回空值。我一直在努力,却看不出问题所在。如果你看到任何东西,请提出建议。谢谢 //DirectoryEntry is a class

我试图读取一个文件,其中一行包含一个名称,第二行后跟数字。第三行是名字,第四行是数字,依此类推

我用main方法测试了它。例如,我的文件包含名称“Bobby”,他的telno是123456,当我运行lookUpEntry(“Bobby”)时,我应该将他的telno返回给我。如果文件的第一个名字是“Bobby”,则此选项有效。如果不是第一个,则程序似乎无法识别名称并返回空值。我一直在努力,却看不出问题所在。如果你看到任何东西,请提出建议。谢谢

//DirectoryEntry is a class and contains a String name and String telno along with set/ get methods for them. 
private DirectoryEntry[] theDirectory = new DirectoryEntry[100];
private int size = 0;

public void loadData(String sourceName) {
        try {
            Scanner in = new Scanner(new File(sourceName));

            while (in.hasNextLine()){
                String name = in.nextLine();
                String telno = in.nextLine();
                theDirectory[size] = new DirectoryEntry(name, telno);
                size++;
            }
            in.close();
        }catch (FileNotFoundException e){
            System.out.println("File not found.");
        }
    }

public String lookUpEntry(String name) {
        find(name);
        if (find(name) >= 0){
            return theDirectory[find(name)].getNumber();
        }
        else{
            return null;
        }
    }

public int find(String name){
        for (int x=0; x < theDirectory.length; x++){
            if (theDirectory[x].getName().equals(name)){
                return x;
            }
            else{
                return -1;
            }
        }
        return  -1;
    }
//DirectoryEntry是一个类,包含字符串名称和字符串telno以及它们的set/get方法。
私有目录项[]目录=新目录项[100];
私有整数大小=0;
public void loadData(字符串sourceName){
试一试{
Scanner in=new Scanner(新文件(sourceName));
while(在.hasNextLine()中){
字符串名称=in.nextLine();
字符串telno=in.nextLine();
目录[大小]=新目录条目(名称,电话号码);
大小++;
}
in.close();
}catch(filenotfounde异常){
System.out.println(“未找到文件”);
}
}
公共字符串查找项(字符串名称){
查找(姓名);
如果(查找(名称)>=0){
返回目录[find(name)].getNumber();
}
否则{
返回null;
}
}
公共整数查找(字符串名称){
对于(int x=0;x
以下是文件内容:

阿兰A

123456

博比B

234567

查理C

456789

丹尼尔D

567891

埃里克·E


787454在find方法中,遍历数组,但使用if、else块。基本上,如果您要查找的名称不在索引0中,代码将跳转到else语句并返回-1

编辑:等等,对不起,我没有看到你在主代码中使用这个函数。。。还是你该修的

编辑2:那不是你的主要方法。。。再抓一次

固定代码:

public int find(String name){
    for (int x=0; x < theDirectory.length; x++){
        if (theDirectory[x].getName() != null && theDirectory[x].getName().equals(name)){
            return x;
        }
    }
    return  -1;
}
public int find(字符串名称){
对于(int x=0;x
在find方法中,遍历数组,但使用if、else块。基本上,如果您要查找的名称不在索引0中,代码将跳转到else语句并返回-1

编辑:等等,对不起,我没有看到你在主代码中使用这个函数。。。还是你该修的

编辑2:那不是你的主要方法。。。再抓一次

固定代码:

public int find(String name){
    for (int x=0; x < theDirectory.length; x++){
        if (theDirectory[x].getName() != null && theDirectory[x].getName().equals(name)){
            return x;
        }
    }
    return  -1;
}
public int find(字符串名称){
对于(int x=0;x
如果要查找的内容不在第一个元素中,则从
公共int find(字符串名称)
方法返回-1

下面是它应该是什么样子

public int find(String name) {

    for (int i=0; i<size; i++)
        if (theDirectory[i].getName() != null && theDirectory[i].getName().equals(name))
            return i;
    return -1;
}
public int find(字符串名称){

对于(int i=0;i如果要查找的内容不在第一个元素中,则从
public int find(String name)
方法返回-1

下面是它应该是什么样子

public int find(String name) {

    for (int i=0; i<size; i++)
        if (theDirectory[i].getName() != null && theDirectory[i].getName().equals(name))
            return i;
    return -1;
}
public int find(字符串名称){

for(int i=0;i使用find中的else语句。它在第一次检查后返回-1。find应为:

public int find(String name){
    for (int x=0; x < theDirectory.length; x++){
        if (theDirectory[x].getName() != null && theDirectory[x].getName().equals(name)){
            return x;
        }
    }
    return  -1;
}
public int find(字符串名称){
对于(int x=0;x
使用find中的else语句。它在第一次检查后返回-1。find应为:

public int find(String name){
    for (int x=0; x < theDirectory.length; x++){
        if (theDirectory[x].getName() != null && theDirectory[x].getName().equals(name)){
            return x;
        }
    }
    return  -1;
}
public int find(字符串名称){
对于(int x=0;x
首先,我要指出,您正在检查文件是否有下一行,但随后您将获得下两行,而不检查第二行是否存在。大小写或空格如何?请查看
时发生的情况。equalsIgnoreCare(name.trim())
。将代码张贴在调用这些方法的地方,以及要读取的文件的示例。首先,我要指出,您正在检查文件是否有下一行,但随后您将获得下两行,而不检查第二行是否存在。大小写或空格如何?请查看
时发生的情况。equalsIgnoreCare(name.trim())
。在调用这些方法的地方发布代码,并给出一个要读取的文件的示例。他在lookUpEntry()方法中使用了find()方法三次。是的,我刚刚意识到。我以为第一个方法是他的主要方法。愚蠢的错误,最后用了一小时的时间盯着屏幕。Tnks。他正在使用find()方法方法在他的lookUpEntry()方法中使用了三次。是的,我刚刚意识到了这一点。我以为第一个方法是他的主要方法。愚蠢的错误,最后用了一个小时的时间盯着屏幕。Tnks。