1
\$\begingroup\$

I'm an amateur programmer who just started learning programming, my school gave me an assignment to create code that reads a surname and lastname from an input dialog box and then to edit those name in the format of "[email protected]" all of wich should be in lowercase letters and only letters of a-z since in sweden we sometimes use "å,ä,ö" in our names. I have managed to do so, as you can see below but i was wondering if there was a simpler way to do this (perhaps by using less code).

import javax.swing.*;
public class Namn {
    public static void main(String[] arg) {
        char c = '.';
        String s = JOptionPane.showInputDialog(null, "Ange namn och efternamn");
        s = s.replaceAll("\\s{2,}", " ").trim();
        s = s.replace(' ', c);
        s = s.toLowerCase();
        s = s.replace('å', 'a');
        s = s.replace('ä', 'a');
        s = s.replace('ö', 'o');
        JOptionPane.showMessageDialog(null, s + "@nackademin.se");
        System.exit(0);
    }
}
\$\endgroup\$
0

4 Answers 4

2
\$\begingroup\$

You can chain replaces and you don't need to replace "n. spaces" -> " ", you can just replace them with a "." if you trim first

String s = JOptionPane.showInputDialog(null, "Ange namn och efternamn");
s = s.trim()
      .replaceAll("\\s{2,}", ".")
      .toLowerCase()
      .replace('å', 'a')
      .replace('ä', 'a')
      .replace('ö', 'o');
JOptionPane.showMessageDialog(null, s + "@nackademin.se");
\$\endgroup\$
2
\$\begingroup\$

Removing Diacritics:

Do not try and replace all the various diacritics like with separate replacements.

For UNICODE strings there is a idiomatic way to do it built in:

import static java.text.Normalizer.normalize;

System.out.println(normalize("å,ä,ö", Normalizer.Form.NFKD).replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""));

Outputs:

a,a,o

Note:

This will not help transliterate stand alone characters like "ø" ( LATIN SMALL LETTER O WITH STROKE character (U+00F8) ).

You will need to use an actual transliteration mapping library for stand alone characters like that.

\$\endgroup\$
0
1
\$\begingroup\$

You can get rid of the null object on your showInputDialog().

String s = JOptionPane.showInputDialog("Ange namn och efternamn");

The signature that you're using is:

showInputDialog(Component parentComponent, Object message)

According to the Java Documentation:

Shows a question-message dialog requesting input from the user parented to parentComponent.

This simply means that the dialog would be displayed on top of the Component's frame. But since you are passing null, you can instead use:

showInputDialog(Object message)

Shows a question-message dialog requesting input from the user. The dialog uses the default frame, which usually means it is centered on the screen.

\$\endgroup\$
0
\$\begingroup\$

Name Parsing Edge Case:

Your whitespace handling is not generating what your requirements imply.

.replaceAll("\\s{2,}", "").trim().replace(' ', '.')

will not result in what you require if either the first name or the last name contain a space in the name.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.