0

I have a text file with this structure:

CRIM:Continuius
ZN:Continuius
INDUS:Continuius
CHAS:Categorical
NOX:Continuius   

I inserted it into a two dimensional array:

BufferedReader description = new BufferedReader(new FileReader(fullpath2));
        String[][] desc;
        desc = new String[5][2];

        String[] temp_desc;
        String delims_desc = ":";
        String[] tempString;

        for (int k1 = 0; k1 < 5; k1++) {
            String line1 = description.readLine();
            temp_desc = line1.split(delims_desc);
            desc[k1][0] = temp_desc[0];
            desc[k1][1] = temp_desc[1];
        }

and then tried to identify which attribute is Categorical:

        String categ = "Categorical";
        for (int i=0; i<5; i++){
            String temp1 = String.valueOf(desc[i][1]);
            if ("Categorical".equals(desc[i][1])){
                System.out.println("The "+k1+ " th is categorical.");
}
}

Why doesn't it return true, although one of the attributes is categorical?

12
  • it's either a case problem, in which case use equalsIgnoreCase or you don't have the input you think you do Commented Jun 7, 2013 at 20:48
  • 5
    In any case, run a debugger on that. You'll know soon enough. Commented Jun 7, 2013 at 20:49
  • Newline symbol at the end of line1? Commented Jun 7, 2013 at 20:49
  • WHy an array, why not a Map? Commented Jun 7, 2013 at 20:49
  • What happens when you print all the values? Do they match what you expect? Commented Jun 7, 2013 at 20:49

1 Answer 1

3

Looking at the input you posted (in the edit perspective), I saw there is a lot of trailing whitespace on almost every line of the textfile. Your problem will disappear if you replace

desc[k1][1] = temp_desc[1];

with

desc[k1][1] = temp_desc[1].trim();

You could even shorten your code to

for (int k1 = 0; k1 < 5; k1++) {
    String line1 = description.readLine().trim();
    desc[k1] = line1.split(delims_desc);
}

Clarification:

You are trying to compare

"Categorical" // 11 characters

with

"Categorical        " // more than 11 characters

and those are not equal strings.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your solution. Since there is no space between two words, it is already trimmed.
@VTT: As said in my answer: the extra whitespace is at the end of the line. So you are trying to compare "Categorical" with "Categorical " and those are not equal strings.
Sorry. You are absolutely right. I fixed it, but still has problem.
@VTT: I posted a solution to the problem you posted. I can not help you further, because "but still has problem" isn't specific enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.