0

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. Though for some reason it is returning false everytime. 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());

    }
}
Pino
  • 7,468
  • 6
  • 50
  • 69
excedion
  • 327
  • 2
  • 5
  • 15
  • 1
    Are you familiar with a debugger? Debugging you program will tell you whats wrong much more quickly than posting it here... Well a little more quickly at least. – Thihara Apr 17 '13 at 05:35

2 Answers2

0

Take a look at my own question I posted a while back. I ended up using Apache Lucene's tokenizer.

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();
    }
Community
  • 1
  • 1
jelgh
  • 705
  • 1
  • 6
  • 22
0

this code will give true only if the file is doc.

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

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

if file name file.txt then what happend

      (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 ==

aymankoo
  • 653
  • 6
  • 12