'Trying figure out how to change greek letter case in Java URL Class

In my code I need to figure out how to change greek letters from capital case to lower case and vice versa. I was using replace but of course, it just ends up making everything upper case. Trying to figure out the logic.

Any help would be greatly appreciated. It prints the whole html in the website thats correct at least.

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class URLParse {
    public static void main(String[] args) {
        String line;
                try {

                    URL url = new URL(args[0]);
                    BufferedReader readr =  new BufferedReader(new InputStreamReader(url.openStream()));

                    BufferedWriter writer =
                            new BufferedWriter(new FileWriter("out.html"));



                    while ((line = readr.readLine()) != null) {

//                        line = line.replace("Α", "α");

//                        line = line.replace("α", "Α");

                        writer.write(line);
                    }

                    readr.close();
                    writer.close();
                    System.out.println("Successfully Downloaded.");


                    String protocol = url.getProtocol();
                    String userinfo = url.getUserInfo();
                    String host = url.getHost();
                    int port = url.getPort();
                    String path = url.getPath();
                    String query = url.getQuery();

                    System.out.println("URL created: " + url);
                    System.out.println("protocol: " + protocol);
                    System.out.println("userinfo: " + userinfo);
                    System.out.println("host: " + host);
                    System.out.println("port: " + port);
                    System.out.println("path: " + path);
                    System.out.println("query: " + query);

                }

                catch (MalformedURLException e) {
                    System.out.println("Malformed URL: " + e.getMessage());
                }
                catch (IOException ie) {
                    System.out.println("IOException raised");
                }
    }

}



Solution 1:[1]

Your logic is problematic. You first replace all upper case "A" to lover case "a". So now you have all "a"s the ones that used to be upper case and the ones that were lover case as lower case "a". And next replacement you change them all to upper case "A". What you need to do is in your first replacement change upper case "A" to some letter or sequence that is never to be found in your text. Then replace lover case "a" to upper case "A" and then replace your "strange" letter or sequenceto lower case "a"

                        String tempReplacement = " ***My-wierd-sequence*** ";
                        line = line.replace("?", tempReplacement );
                        line = line.replace("?", "?");
                        line = line.replace(tempReplacement, "?");

Solution 2:[2]

To flip the case of all Greek letters, you can do the following:

Pattern greek = Pattern.compile("\\p{IsGreek}+");

while ((line = readr.readLine()) != null) {
    line = greek.matcher(s).replaceAll(mr -> flipCase(mr.group()));
    writer.write(line);
}

// . . .

String flipCase(String s) {
    return s.codePoints().map(c -> Character.isLowerCase(c) ?
            Character.toUpperCase(c) : Character.toLowerCase(c))
        .collect(StringBuilder::new, StringBuilder::appendCodePoint,
            StringBuilder::append)
        .toString();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Gantman
Solution 2