I am new to programming Java and working on an assignment involving strings. I just started on using a template of code and my instructions were to compile it and see the output, but there is an error saying "variable phraseLength might not have been initilized". It is referring to when I used "phraseLength" as a variable near the bottom of my code. Does anyone know how to fix this? Thank you very much in advance.
import java.util.Scanner;
public class Working_With_Strings
{
public static void main (String[] args)
{
//instantiate the String……
String phrase = new String ("This is a String test.");
int phraseLength;
int middle3;// number of characters in the phrase String
int middleIndex; // index of the middle character in the String
String firstHalf; // first half of the phrase String
String secondHalf; // second half of the phrase String
String switchedPhrase; // a new phrase with original halves switched
// 1-compute the length and middle index of the phrase
//2- get the substring for each half of the phrase
//3- concatenate the firstHalf at the end of the secondHalf
// print information about the phrase
System.out.println();
System.out.println ("Original phrase: " + phrase);
System.out.println ("Length of the phrase: " + phraseLength +
" characters");
System.out.println ("Index of the middle: " + middleIndex);
System.out.println ("Character at the middle index: " +
phrase.charAt(middleIndex));
System.out.println ("Switched phrase: " + switchedPhrase);
System.out.println();
}
}
I did not understand what the error meant at first because I did not know what it meant by initialization. Now I know that I need to initialize "phraseLength" with a value. Thanks again for all your help!