简体   繁体   中英

Java - variable “st” might not have been initialized

I am getting this error " variable "st" might not have been initialized " in case 5 when i try to do a string count. I tried looking online for a solution, but couldn't find someone with the same problem using string tokenizer. Please could someone tell me why this is happening?

/**
 *To change this license header,choose License Headers in Project Properties.
 *To change this template file,choose Tools|Templates
 *and open the template in the editor.
 */

package labone;

import java.util.Scanner;
import java.util.StringTokenizer;

public class LabOne {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Scanner userInput = new Scanner(System.in);

        System.out.println("Welcome To The String Editor!");
        System.out.println("");
        System.out.println("Please choose what you would like to do by choosing one of the options below:");
        System.out.println("1. Input String");
        System.out.println("2. Print Current String");
        System.out.println("");
        int userOption = 0;
        String stringInput = new String();

        while (userOption != 9) {
            userOption = userInput.nextInt();
            userInput.nextLine();


            switch (userOption) {
                case 1:
                    stringInput = userInput.nextLine();
                    System.out.println(stringInput);
                    break;

                case 2:
                    System.out.println(stringInput);
                    break;

                case 3:
                    stringInput = new StringBuilder(stringInput).reverse().toString();
                    System.out.println(stringInput);
                    break;

                case 4:
                    StringTokenizer st = new StringTokenizer(stringInput);
                    System.out.println(stringInput);
                    break;

                case 5:
                    System.out.println("Number of tokens:" + st.countTokens());
                    break;
                default:
                    ;
                    break;
            }

        }

        // TODO code application logic here
    }

}

Please could someone tell me why this is happening?

You initialize st locally in case 4. It is not visible in case 5.

Solution: initialize it outside the switch block.

Alternatively, as spotted by Thilo , you can initialize it in case 5, as it doesn't seem to be used anywhere else.

    case 5: 
        StringTokenizer st = new StringTokenizer(stringInput);
        System.out.println("Number of tokens:" + st.countTokens());
        break; 

You should be careful not to initialize so many variables locally though, as you can have the very same problem happen again.

Initialize the StringTokenizer outside the switch block. Dont do it in case 4.

You have initialize st in case 4 but not in case 5 , You can corrected it in this way.

    StringTokenizer st;
    switch (userOption) {
         case 1:
           stringInput = userInput.nextLine();
           System.out.println(stringInput);
           break;
         case 2:
           System.out.println(stringInput);
           break;
         case 3:
           stringInput = new StringBuilder(stringInput).reverse().toString();
           System.out.println(stringInput);
           break;
         case 4:
           st = new StringTokenizer(stringInput);
           System.out.println(stringInput);
           break;
         case 5:
           st = new StringTokenizer(stringInput);
           System.out.println("Number of tokens:" + st.countTokens());
           break;
         default:
               ;
           break;
        }

declare it globally, declare it outside the switch block, where you declare your all variable. Declare it here,

int userOption = 0;
String stringInput = new String();
StringTokenizer st; //add here

so your case 4 will be

case 4: st = new StringTokenizer(stringInput);
             System.out.println(stringInput);
              break;

you initialized st in case 4, just take the case when user enter option 5 before 4, in that case st is not initialized right. So the solution is to initialize st either in the main first and then use it in specific switch cases, or initialize it for case 5 as well.

 case 4: StringTokenizer st = new StringTokenizer(stringInput); System.out.println(stringInput); break; case 5: System.out.println("Number of tokens:" + st.countTokens()); break; 

In the switch case, the variable, st declared in case 4 is available in locally for that case only. It will not be available outside the case block. If you need to access it, you need to declare it outside the switch block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM