1

Trying to get a message to appear that is exactly 10 characters long. Can someone point me in the right direction of what to do when the message is SHORTER than 10 characters?

Here is my code so far:

System.out.println("Word:");
String word = scan.nextLine();

String shortword = word.substring(0, 10);   
String shortworduppercase = shortword.toUpperCase();
System.out.println(shortworduppercase); 

I know it has something to do with padRight but I can't seem to get anything to work.

5
  • Could you explain what you are trying to accomplish? Commented Sep 29, 2015 at 20:12
  • It depends on how you want to fill it Commented Sep 29, 2015 at 20:14
  • Also, can you remove the first word (steg...aphy) from the title? While you may be working on code in the context of a such a project, this question and code has nothing directly to do with it, so having the term in the title will just clutter searches for that term. Commented Sep 29, 2015 at 20:20
  • Trying to find out ways to make it so that if you enter a word that is shorter than 10 characters, the program automatically fills the characters that are missing from the 10. So if you enter a 5 letter word, the program enters 5 more to make it 10. Commented Sep 29, 2015 at 20:28
  • Thanks for the title edit. By "the program enters 5 more", do you mean the program appends some constant (like spaces, as in @JohnBollinger's answer), or that the program prompts the user for more input? Commented Sep 29, 2015 at 20:31

2 Answers 2

2

If you are asking how to obtain a string that is padded and / or truncated to exactly 10 characters, then there is a great number of ways to do it. For example, here's one that handles all non-null cases without depending on any (explicit) conditional logic:

String getTenChars(String input) {
    return new StringBuilder(input).append("          ").subString(0, 10);
}
Sign up to request clarification or add additional context in comments.

Comments

1

If the length of your string is shorten than the end index that you're trying to substring you will get java.lang.StringIndexOutOfBoundsException.

In this case you need to check the length of your string before substring it

int maxLength = 10;
String shortWord;
if (word.length() <= maxLength){
    shortWord = word;
} else {
    shortWord = word.substring(0,maxLength);
}
System.out.println(shortWord);

And if you really need a 10 length string, you can use setLength method of StringBuilder class.

For example:

int maxLength = 10;
StringBuilder shortWord;
if (word.length() <= maxLength) {
    shortWord = new StringBuilder(word);
    shortWord.setLength(10);
} else {
    shortWord = new StringBuilder(word.substring(0, maxLength));
}

Comments

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.