3

So I have String array

String[] faculties = new String[21];

and I need to fill it with the names of faculties from .txt file that looks like

Math & CS; First Speciality; First Program; etc

Math & CS; Second Speciality; First Program; etc

Math & CS; Second Speciality; Second Program; etc

Physics; First Speciality; First Program; etc

Physics; First Speciality; Second Program; etc

I use the next code to get these names

FileReader input = new FileReader("faculties1.txt");
BufferedReader bufRead = new BufferedReader(input);
String myLine;
int f = 0;

    while((myLine = bufRead.readLine())!= null){
        String[] arr = myLine.split(";");

        if(f == 0){
            faculties[f] = arr[0];
            f++;
        }
        else{
            if(!faculties[f-1].trim().equals(arr[0].trim())){
                faculties[f] = arr[0];
                f++;
            }
        }
    }

but when I try to check my array

for(int i = 0; i < f; i++){
        System.out.println(faculties[i]);
    }

console says me

Math & CS

Math & CS

Physics

I dont get why Java puts "Math & CS" second time to my array.

6
  • 1
    Did you try to step through it with a debugger? Commented May 23, 2017 at 12:58
  • @litelite yep. this if(!faculties[f-1].trim().equals(arr[0].trim())) returns true when f == 1 even if arr[0] == "Math & CS" and faculties[f-1] == "Math & CS" Commented May 23, 2017 at 13:01
  • 3
    Maybe some kind of invisible unicode caracter somewhere in the file? You can try to open the file with something like notepad++ or sublime text and activate the show all caracters option. Commented May 23, 2017 at 13:03
  • 3
    Try doing a String.compare on faculties[f-1] and arr[0]? If that returns anything other than 0, there's a character issue, as @litelite says Commented May 23, 2017 at 13:05
  • 2
    @litelite thank you much! I used UTF-8 .txt in my project to see cyrillic symbols in console, but when I changed .txt to ANSI console said me Math & CS Physics Commented May 23, 2017 at 13:07

1 Answer 1

1

I tried this it is worked fine and output returns as you expected I think, anyhow you need to throws exceptions.

Math & CS
Physics

According to me there can be,

mispelled character in your text file. Check using different text-editor rather than windows notepad.

UPDATE:

After added this answer I saw your last comment in question that your problem solved.

I am adding this comment because if someone did not see your comment this will be helpful to them.

I used UTF-8 .txt in my project to see cyrillic symbols in console, but when I changed .txt to ANSII console said me Math & CS Physics*

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

1 Comment

Affter added this answer I saw your last comment in question that your problem solved.

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.