1

Program overview: Ask user for phrase, ask user for an index in which a scramble will rotate the phrase until that letter at index is the first index (0) of the string. Ask for an Integer until an integer is given. After phrase is scrambled ask to scramble again. if yes, scramble to inputted index, if no, print final result end program.

import java.util.Scanner;

public class PJ {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    String phrase;
    String phraseMut;
    int index;

    System.out.println("Enter your word or phrase: ");
    phrase = scan.nextLine();
    phraseMut = phrase;
    System.out.println();

    while (true) {
      System.out.println("Enter the index of a character in your phrase: ");
      if (scan.hasNextInt()) {
        index = scan.nextInt();
        scan.nextLine();
        break;
      } else if (index >= phraseMut.length()) {
        System.out.println("Error: Index is Double not Integer.");
      } else {
        System.out.println("Error: Index is not Integer.");
      }
    }

    System.out.println();
    System.out.println("Rotating phrase to bring index " + index + " to the front. . .");

    int count = 0;

    for (int i = 0; i < index; i++) {
      phraseMut = phraseMut.substring(1, phrase.length()) + "" + phraseMut.substring(0, 1);
      count++;
      System.out.println(phraseMut);
    }
  }
}

First for loop, I cannot access "index" as the compiler is stating it may not have been initialized. Any solution to access this?

7
  • 2
    The compiler is not able to infer the fact that within the while-loop, index must be set in order to exit the loop. Therefore, we have to initialize index. Easiest way is to initialize it at declaration: int index = 0; Commented Nov 8, 2021 at 22:59
  • @Turing85 The variable index is declared earlier in the method: int index;. Commented Nov 8, 2021 at 22:59
  • 2
    @BasilBourque declaration is not equal to initialization. Commented Nov 8, 2021 at 23:01
  • @Turing85 It is for a primitive. Commented Nov 8, 2021 at 23:02
  • 2
    No, it is only for fields, not for local variables. Ideone demo Commented Nov 8, 2021 at 23:02

2 Answers 2

2

Your line

else if (index >= phraseMut.length()) {

may be reading index before it's been set. In particular, if the Scanner doesn't see an int. You need to think about what value you want index to have, when that happens. Like 0 maybe, or -1.

Then set index to that when you declare it. So maybe you'd change int index; to int index = 0; for example, if that's what you decide you need index to be in the case where a value is not read from the Scanner.

Sign up to request clarification or add additional context in comments.

1 Comment

When initializing an index variable like this, I typically set it to -1 so that it can't be mistaken for the first index in an array or string. In this case, if phraseMut.length() == 0 then there might be an error displayed when it's a false positive for that error.
0

The Answer by Kareem is correct.

To explain a bit more:

  • Member fields of a primitive type such as int are automatically assigned a default value. The list of defaults is shown in the Primitive Data Types tutorial by Oracle.
  • Local variables of a primitive type such as int are not assigned a default value.

To quote the tutorial linked above:

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

For an example, see code run live at IdeOne.com, as posted by Turing85.

int i;
System.out.println(i);

error: variable i might not have been initialized System.out.println(i);

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.