Learning_Java
Learning_Java

Reputation: 65

How to print the middle three characters of a String in Java?

I am new to programming and working on a Java String assignment. The question says:

  1. Declare a variable of type String named middle3 (put your declaration with the other declarations near the top of the program) and use an assignment statement and thesubstring method to assign middle3 the substring consisting of the middle three characters of phrase (the character at the middle index together with the character to the left of that and the one to the right). Add a println statement to print out the result. Save, compile, and run to test what you have done so far.

Here is what I have done:

String phrase = new String ("This is a String test.");
String middle3 = new String ("tri"); //I want to print the middle 3 characters in "phrase" which I think is "tri".

middle3 = phrase.substring (9, 11); // this only prints out 1 letter instead of 3


System.out.println ("middle3: " + middle3);

Here is my output:

Original phrase: This is a String test.
Length of the phrase: 18 characters
middle3:  S

I also think that there are 18 characters in the string "phrase", but if there isn't, please let me know. Thank you in advance for all your help!

Upvotes: 1

Views: 34128

Answers (3)

Mshnik
Mshnik

Reputation: 7042

First off, there are 22 characters in phrase.

"This is a String test."
 ^^^^^^^^^^^^^^^^^^^^^^ --> 22 

Note that spaces () count towards the character count. Additionally, String has a method length() which will give you this number.

String phrase = "This is a String test.";
int phraseLength = phrase.length(); //22

From there, we can get the middle three characters by working off of the value phraseLength/2. The middle three characters would start one before the middle position, and stop one afterwards. However, because the string(int, int) method takes the end index to be exclusive, we should increase it by one.

String phrase = "This is a String test.";
int phraseLength = phrase.length(); //22
String middle3 = phrase.substring(phraseLength/2 - 1, phraseLength/2 + 2); // will have the middle 3 chars.

If the length of phrase is odd, this will return it middle 3 chars. If the length of phrase is even (as it is here), this will return the left-middle 3 chars. (For example, in 1,2,3,4, I'm using left-middle to represent 2 and right-middle to represent 3).

Another note, it's both unnecessary and bad practice to write new String("asdf"). Simply use the string literal instead.

String phrase = new String ("This is a String test."); //Bad
String phrase = "This is a String test."; //Good

Upvotes: 2

Chris Gong
Chris Gong

Reputation: 8229

Think about how to retrieve the middle 3 characters without hardcoding the bounds of the substring() method. You can use the length() method in this regard. For example, the middle character of a string of odd length will always be at index str.length()/2, while the two middle characters of a string of even length will always be at index str.length()/2 and (str.length()/2) - 1. So the definition of the three middle characters will be up to you. But for our sakes, we'll just make the 3 middle characters at indexes (str.length()/2)-1, str.length()/2, and (str.length()/2)+1. With this information you can modify this line of code you had before,

middle3 = phrase.substring (9, 11);

to

middle3 = phrase.substring (phrase.length()/2 - 1, phrase.length()/2 + 2);

As to why the original line of code you had before was returning only one letter, it has to do with the parameters of the substring method. The first parameter is inclusive, but the second parameter isn't. Therefore, you were only retrieving characters from 9 to 10.

This is a String test.
         ^^^
        9,10,11 (11 is not included)

The three characters I pointed to are at indices 9, 10, and 11 respectively. You only retrieved the chars ' ' and 'S', which together is just " S". That explains the one-letter output before.

Upvotes: 5

Rubén Soler
Rubén Soler

Reputation: 1187

you have an error in the begind index and the end index in the string, this is the correct code:

 String phrase = new String("This is a String test.");
 String middle3 = phrase.substring(11, 14); 
 System.out.println("middle3: " + middle3);

Upvotes: 2

Related Questions