2014-04-24 52 views
0

我做电话目录的项目,我们有扫描仪似乎并没有被阅读文件

我使用扫描仪来加载从数据从一个目录文件telnos.txt阅读文件telnos.txt,使用来自上一个问题的loadData方法,我在这里问StackOverflow。

我注意到试图找到一个用户总是返回未找到,所以我在方法中添加了一些System.out.printlns,以帮助我看看发生了什么。它看起来像扫描仪没有从文件中读取任何东西。奇怪的是,它将文件的名称打印为应读取的第一行,这让我觉得我错过了一些非常简单的东西。

控制台

ArrayPhoneDirectory.java

import java.util.*; 
import java.io.*; 

public class ArrayPhoneDirectory implements PhoneDirectory { 
    private static final int INIT_CAPACITY = 100; 
    private int capacity = INIT_CAPACITY; 

    // holds telno of directory entries 

    private int size = 0; 

    // Array to contain directory entries 

    private DirectoryEntry[] theDirectory = new DirectoryEntry[capacity]; 

    // Holds name of data file 

    private final String sourceName = "telnos.txt"; 
    File telnos = new File(sourceName); 

    // Flag to indicate whether directory was modified since it was last loaded or saved 

    private boolean modified = false; 

    // add method stubs as specified in interface to compile 

    public void loadData(String sourceName) { 
     Scanner read = new Scanner("telnos.txt").useDelimiter("\\Z"); 
     int i = 1; 
     String name = null; 
     String telno = null; 
     while (read.hasNextLine()) { 
      if (i % 2 != 0) 
       name = read.nextLine(); 
      else 
       telno = read.nextLine(); 
      add(name, telno); 
      i++; 
     } 
    } 

    public String lookUpEntry(String name) { 
     int i = find(name); 
     String a = null; 
     if (i >= 0) { 
      a = name + (" is at position " + i + " in the directory"); 
     } else { 
      a = ("Not found"); 
     } 
     return a; 
    } 

    public String addChangeEntry(String name, String telno) { 
     for (DirectoryEntry i : theDirectory) { 
      if (i.getName().equals(name)) { 
       i.setNumber(telno); 
      } else { 
       add(name, telno); 
      } 
     } 
     return null; 
    } 

    public String removeEntry(String name) { 
     for (DirectoryEntry i : theDirectory) { 
      if (i.getName().equals(name)) { 
       i.setName(null); 
       i.setNumber(null); 
      } 

     } 
     return null; 
    } 

    public void save() { 
     PrintWriter writer = null; 
     // writer = new PrintWriter(FileWriter(sourceName)); 

    } 

    public String format() { 
     String a; 
     a = null; 
     for (DirectoryEntry i : theDirectory) { 
      String b; 
      b = i.getName() + "/n"; 
      String c; 
      c = i.getNumber() + "/n"; 
      a = a + b + c; 

     } 
     return a; 
    } 


    // add private methods 


    // Adds a new entry with the given name and telno to the array of 
    // directory entries 
    private void add(String name, String telno) { 
     System.out.println(name); 
     System.out.println(telno); 
     theDirectory[size] = new DirectoryEntry(name, telno); 
     size = size + 1; 
    } 


    // Searches the array of directory entries for a specific name 
    private int find(String name) { 
     int result = -1; 
     for (int count = 0; count < size; count++) { 

      if (theDirectory[count].getName().equals(name)) { 
       result = count; 
      } 
      System.out.println(result); 
     } 
     return result; 
    } 

    // Creates a new array of directory entries with twice the capacity 
    // of the previous one 
    private void reallocate() { 
     capacity = capacity * 2; 
     DirectoryEntry[] newDirectory = new DirectoryEntry[capacity]; 
     System.arraycopy(theDirectory, 0, newDirectory, 
       0, theDirectory.length); 

     theDirectory = newDirectory; 
    } 


} 

ArrayPhoneDirectoryTester.java

import java.util.Scanner; 

public class ArrayPhoneDirectoryTester { 
    public static void main(String[] args) { 
     //create a new ArrayPhoneDirectory 
     PhoneDirectory newTest = new ArrayPhoneDirectory(); 

     newTest.loadData("telnos.txt"); 
     System.out.println("loadData tested successfully"); 
     System.out.print("Please enter a name to look up: "); 
     Scanner in = new Scanner(System.in); 
     String name = in.next(); 
     String entryNo = newTest.lookUpEntry(name); 
     System.out.println(entryNo); 


    } 

} 

telnos.txt

John 
123 
Bill 
23 
Hello 
23455 
Frank 
12345 
Dkddd 
31231 
+0

在哪里你的文件位于? – manan

+0

你可以尝试将这个问题减少到更小的东西并分享吗?通常减少问题可帮助您自己解决问题! – pamphlet

+0

Try Scanner read = new Scanner(new File(“telnos.txt”)); – manan

回答

0

在您的代码:

Scanner read = new Scanner("telnos.txt"); 

不会加载文件 'telnos.txt' 。而是要创建一个扫描器对象来扫描字符串“telnos.txt”。 为了使扫描仪明白,它能够扫描你有一个文件要么:

Scanner read = new Scanner(new File("telnos.txt")); 

或创建一个文件对象,并通过其路径扫描仪的构造。

如果您收到“文件未找到”错误,您需要检查当前的工作目录。你可以运行下面的行,看看你确实是在正确的目录中,该文件是:

String workingDir = System.getProperty("user.dir"); 
System.out.println("Current working directory : " + workingDir); 

您还需要赶上FileNotFoundException异常的功能如下:

public void loadData(String sourceName) { 
     try { 
     Scanner read = new Scanner(new File("telnos.txt")).useDelimiter("\\Z"); 
     int i = 1; 
     String name = null; 
     String telno = null; 
     while (read.hasNextLine()) { 
      if (i % 2 != 0) 
       name = read.nextLine(); 
      else { 
       telno = read.nextLine(); 
       add(name, telno); 
      } 
      i++; 
     } 
     }catch(FileNotFoundException ex) { 
     System.out.println("File not found:"+ex.getMessage); 
     } 
    } 
+0

谢谢,我现在正在做,给我一个文件没有发现异常 – Sam

+0

你需要检查当前工作目录。编辑我的答案,以检查当前工作目录的步骤。如果它是其他目录,您将知道如何修改文件路径。 – user2881767

+0

@Sam同样来自您的代码,您将不得不将添加(name,telno)移动到else块中,否则您读取的名称和telno之间将会出现不匹配。 – user2881767