-3

I am new to Java and I am trying make the hashtags to adjust to my text. For example, if I write "message hello, how are you?" I want it to print with capital letters and that the hashtags adjust themselves depending on how many characters I print. Do you have any suggestions on what I can use to make this happen?

public void addMessage() {
    System.out.println("Write message followed by a text: ");
    String message = readString();
    System.out.println("############################################################");
    System.out.println("#                                                          #");
    System.out.println("#" + message.substring(7).toUpperCase() + "                 #");
    System.out.println("#                                                    #");
    System.out.println("############################################################");
}
Tobb
  • 10,619
  • 4
  • 48
  • 67
P. Jones
  • 1
  • 1
  • 7
    For your information, the character "#" is called "hash", not "hashtag". – Stephen C Jan 04 '17 at 10:39
  • Use [`String#lenght()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--) on `message.substring(7)` and some basic math to calculate the number of hashes you want to print. – Travis Jan 04 '17 at 10:41
  • 1
    You could use [`String.format()`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)). – MC Emperor Jan 04 '17 at 10:42
  • 1
    And you should check that `message` has at least 7 characters before invoking `substring(7)`. – Marvin Jan 04 '17 at 10:44
  • Remember to add example input and output, I see there are multiple interpretations of what you are after in the answers provided. – Tobb Jan 04 '17 at 10:47

3 Answers3

0

All you need to do is calculate length of your string and output hashs specified amount of times.

The code below should be helpful.

String hashs(int len) {
    return new String(new char[len]).replace("\0", "#");
}
int textLen = message.length();
System.out.println(hashs(len + 2));
System.out.println("#" + message.toUpperCase() + "#");
System.out.println(hashs(len + 2));
vvg
  • 6,075
  • 14
  • 32
0

Something like this:

public void addMessage() {
    System.out.println("Write message followed by a text: ");
    String message = readString();

    System.out.println(createHashes(input.length() + 2));
    System.out.println("#" + createSpaces(input.length()) + "#");
    System.out.println("#" + input.toUpperCase() + "#");
    System.out.println("#" + createSpaces(input.length()) + "#");
    System.out.println(createHashes(input.length() + 2));
}

private String createHashes(final Integer numberOfHashes) {
    return new String(new char[numberOfHashes]).replace("\0", "#");
}

private String createSpaces(final Integer numberOfSpaces) {
    return new String(new char[numberOfSpaces]).replace("\0", " ");
}

Example input/output:

input: Hey, you!

output:

###########
#         #
#HEY, YOU!#
#         #
###########

input: How you doin'?

output:

################
#              #
#HOW YOU DOIN'?#
#              #
################
Tobb
  • 10,619
  • 4
  • 48
  • 67
0

You can use the Formatter.

    String[] strings  = {"one", "two", "three"};
    for(String item: strings){
        System.out.printf("#%20s      #\n", item.toUpperCase());
    }

That way the string print will always have the same width. For this example the output is:

#                 ONE     #
#                 TWO     #
#               THREE     #
matt
  • 8,598
  • 3
  • 19
  • 30