【问题标题】:Tokenizer not separating string? (JAVA)标记器不分隔字符串? (JAVA)
【发布时间】:2013-04-17 13:47:29
【问题描述】:

我有一个名为 File 的类 存储文件大小、名称、驱动器和目录的位置。

该类应该将扩展名与文件名(“java”与“test.java”)分开,然后使用equals方法将其与另一个文件进行比较。尽管由于某种原因它每次都返回错误。知道有什么问题吗?

类文件

import java.util.*;

public class FileLocation
{
    private String name;
    private char drive;
    private String directory;
    private int size;

    public FileLocation()
    {
        drive = 'X';
        directory = "OOProgramming\\Practicals\\";
        name = "test";
        size = 2;
    }

    public FileLocation(char driveIn, String dirIn, String nameIn, int sizeIn)
    {
        drive = driveIn;
        directory = dirIn;
        name = nameIn;
        size = sizeIn;
    }

    public String getFullPath()
    {
        return drive + ":\\" + directory + name;
    }

    public String getFileType()
    {
        StringTokenizer st1 = new StringTokenizer(name, ".");

        return "File type is " + st1.nextToken();

    }

    public String getSizeAsString()
    {
        StringBuilder data = new StringBuilder();

        if(size > 1048575)
        {
            data.append("gb");
        }
            else if(size > 1024)
            {
                data.append("mb");
            }
                else
                {
                    data.append("kb");
                }

        return size + " " + data;
    }

    public boolean isTextFile()
    {
        StringTokenizer st2 = new StringTokenizer(name, ".");

        if(st2.nextToken() == ".txt" || st2.nextToken() == ".doc")
        {
            return true;
        }
            else
            {
                return false;
            }
    }

    public void appendDrive()
    {
        StringBuilder st1 = new StringBuilder(drive);
        StringBuilder st2 = new StringBuilder(directory);

        StringBuilder combineSb = st1.append(st2);
    }

    public int countDirectories()
    {
        StringTokenizer stDir =new StringTokenizer(directory, "//");
        return stDir.countTokens();
    }

    public String toString()
    {
        return "Drive: " + drive + " Directory: " + directory + " Name: " + name + " Size: " + size;
    }

    public boolean equals(FileLocation f)
    {
        return drive == f.drive && directory == f.directory && name == f.name && size == f.size;
    }

}

测试程序

import java.util.*;

public class FileLocationTest
{
    public static void main(String [] args)
    {
        Scanner keyboardIn = new Scanner(System.in);

        FileLocation javaAssign = new FileLocation('X', "Programming\\Assignment\\", "Loan.txt", 1);

        int selector = 0;

        System.out.print(javaAssign.isTextFile());

    }
}

【问题讨论】:

  • 您熟悉调试器吗?调试你的程序会比在这里发布它更快地告诉你什么是错误的......至少快一点。

标签: java string


【解决方案1】:

仅当文件为 doc 时,此代码才会返回 true。

    StringTokenizer st2 = new StringTokenizer(name, ".");

    if(st2.nextToken() == ".txt" || st2.nextToken() == ".doc")

如果文件名 file.txt 那么发生了什么

      (st2.nextToken() == ".txt") means ("file" == "txt") false
      (st2.nextToken() == ".doc") means ("txt" == "txt") false

第一个令牌将给出文件名第二个令牌将给出分机。

正确的代码是

     StringTokenizer st2 = new StringTokenizer(name, ".");
     String filename = st2.nextToken();
     String ext = st2.nextToken();
     if(ext.equalsIgnoreCase(".txt") || ext.equalsIgnoreCase(".txt"))

使用总是等于来比较字符串而不是 ==

【讨论】:

  • 谢谢,当我删除 .来自equals()
【解决方案2】:

看看我不久前发布的my own question。我最终使用了 Apache Lucene 的标记器。

这是你的使用方法(复制自here):

    TokenStream tokenStream = analyzer.tokenStream(fieldName, reader);
    OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
    CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);

    while (tokenStream.incrementToken()) {
        int startOffset = offsetAttribute.startOffset();
        int endOffset = offsetAttribute.endOffset();
        String term = charTermAttribute.toString();
    }

【讨论】:

  • 为这样一个微不足道的任务添加来自 Lucene 的依赖项实在是太过分了,恕我直言。
  • 你说得对,我读的有点快。没看到实际使用情况
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多