0

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!

Learning_Java
  • 65
  • 1
  • 4
  • 12

4 Answers4

2
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

to:

int phraseLength = 0;
int middle3 = 0;// number of characters in the phrase String
int middleIndex = 0;    // 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

you should init the variable before use them...i recommend a book to you Head First Java

Zhang Xiang
  • 422
  • 4
  • 15
2

variable declared in methods can not be initialized automatically。
maybe you need to read some basic books about java basics。

int phraseLength = 0;
int middle3 = 0;// number of characters in the phrase String
int middleIndex = 0;    // index of the middle character in the String
String firstHalf = null;   // first half of the phrase String
String secondHalf = null;  // second half of the phrase String
String switchedPhrase = null; 
hijawa
  • 31
  • 2
1

You have declared a variable and not initialized the variable

int phraseLength;

and here you are trying to access a variable which is not initialized.

System.out.println ("Length of the phrase: " + phraseLength +
                " characters");

You must define the value to the variable before accessing the value:

There are two solutions in your case :

1.You should initialize the variable after declaration like below:

`int phraseLength;//Declaration
pharaseLength = 0;`//Initialization

2.Assign the variable through some calculations :

Here I am taking the length of the string assign it to the variable

  pharaseLength = pharaseString.length();

You need to do the same for remaining variables

For your reference :

Difference between Initialization , assignment ,declaration: https://stackoverflow.com/a/2614178/5395773

Community
  • 1
  • 1
Venkat
  • 2,549
  • 2
  • 28
  • 61
1
`     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;
    phraseLength=phrase.length();
    System.out.println ("Original phrase: " + phrase);
    System.out.println ("Length of the phrase: " + phraseLength +
                " characters");

    }
    }     

           `