D
D
Dmitry Mayorov2021-01-18 07:48:56
Java
Dmitry Mayorov, 2021-01-18 07:48:56

Why doesn't Java compare characters?

I can’t print a line break because the condition is not equal ... That is, with each character (belongs to the String[] type) "~" a line break must occur, for this I break the string into characters (belongs to the String type []), each character (belongs to the String[] type) is compared and if it is not similar to "~", then there is no line break, and if it is similar, then a line break occurs. So, instead of wrapping the line, the program simply displays the character (belongs to the String type) "~".

FileReader fr = new FileReader("Desktop\\"+text[2]);
                  Scanner read_text_file = new Scanner(fr);
                  String[] str = read_text_file.nextLine().split("");
                  System.out.println("|==========================*");
                  fr.close();
                  for(int i = 0; i < str.length;i++){
                     if(str[i] == "~"){
                        System.out.println("");
                     }else{
                        System.out.print(str[i]);
                     }
                  }
                  System.out.println("");
                  System.out.println("|==========================*");

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-01-18
@Dmitry-Mayorov

Because you need to compare with a string through the .equals method.
This is how it will work:

if ("~".equals(str[i])){
                System.out.println("");
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question