简体   繁体   English

分词器不能分隔字符串? (JAVA)

[英]Tokenizer not separating string? (JAVA)

I have a class Called File Location which stores the size, name, drive and directory of a file. 我有一个叫文件位置的类,它存储文件的大小,名称,驱动器和目录。

The class is supposed to separate the extension from the file name ("java" from "test.java") then compare it to another file using an equals method. 该类应该将扩展名与文件名分开(“ test.java”中的“ java”),然后使用equals方法将其与另一个文件进行比较。 Though for some reason it is returning false everytime. 尽管由于某种原因,它每次都返回false。 Any idea what's wrong? 知道有什么问题吗?

Class file 类文件

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;
    }

}

Tester program 测试程序

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());

    }
}

Take a look at my own question I posted a while back. 看看我不久前发布的我自己的问题 I ended up using Apache Lucene's tokenizer. 我最终使用了Apache Lucene的令牌生成器。

Here is how you use it (copied from 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();
    }

this code will give true only if the file is doc. 仅当文件为doc时,此代码才会为true。

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

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

if file name file.txt then what happend 如果文件名为file.txt,那么会发生什么

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

first token will gave file name second token will gave ext. 第一个标记将给文件名,第二个标记将给分机。

right code is 正确的代码是

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

use always equals to compare strings not == 使用始终等于来比较字符串不是==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM