-3

I'm trying to write code that determines whether the characters in a string are alphabetical or not, but I keep getting a

return variable might not have been initialized

error. I'm pretty sure I've accounted for every possibility, so I'm not sure where my code is going wrong.

My code

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Daniel D
  • 3
  • 1
  • 4
    Welcome to Stack Overflow! Please visit the [help] to see what and [ask]. HINT: Post effort and CODE, not PICTURES of code Also [JAVA is NOT JavaScript](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – mplungjan Feb 20 '19 at 06:06
  • @ArunKumar I never vote new users down – mplungjan Feb 20 '19 at 06:09
  • But some crazy users doing these kind of thing :) –  Feb 20 '19 at 06:09
  • 1
    This question is fully answered, in plain English, by the error message – Joakim Danielson Feb 20 '19 at 06:20
  • int cannot be null in java – MozenRath Feb 20 '19 at 06:25
  • This issue is already answered many times (one has to do a Google search like "java return variable might not have been initialized"). Here are couple of them: [Variable might not have been initialized error](https://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) and [Why does the Java compiler not understand this variable is always initialized?](https://stackoverflow.com/questions/13235559/why-does-the-java-compiler-not-understand-this-variable-is-always-initialized). – prasad_ Feb 20 '19 at 06:43
  • @prasad_ , StackOverflow requires code to be posted, and if every code was same, there would be no questions, that is why this question is not a duplicate, as it has its own code. and the OP would never know that he has to search the question you have linked to. He is a new guy. just help him out in the best way possible and move on. – MozenRath Feb 20 '19 at 08:30
  • @JoakimDanielson , answering a question should be in a manner that the person asking the question should never ask a similar question again. That's why I have posted the answer. I believe this question is worth asking and answering. – MozenRath Feb 20 '19 at 08:32

2 Answers2

1

What's missing is that you need to initialize the boolean variable alphabetical here as you have not provided a else in the conditions for your outermost if-else conditions. Although you have covered all the possible paths logically, you need to let the compiler know of the same too. The compiler is worried that there might be a code path in which alphabetical is never initialized as there is no else or default condition specified.

There are two ways to resolve your issue:

1> The alphabetical should be set to a default value(which will never be used)

2> replace the else if(length!=1) with simply else

MozenRath
  • 9,652
  • 13
  • 61
  • 104
  • 1
    Good answer. I posted a similar one, but you beat me to the punch, so I deleted mine. I just wanted to make it clear to the OP that he was correct in his thinking that he was covering every possibility, and that it was just the way he did it (with the unnecessary final test) that caused the compiler to not be sure he had. – CryptoFool Feb 20 '19 at 06:35
  • Thanks! I do not know why many people did not even like the question... – MozenRath Feb 20 '19 at 08:25
0

You have not initialised variable alphabetical with any default value. Assign a default value to it either true or false. JVM is not quite sure that during execution one if will met the condition true so it is giving this message.

Ankit Agrawal
  • 596
  • 5
  • 12