0

I'm assigning my variables in an if statement and trying to use them outside of it, but I can't figure out how. The error is in the very last if statement stating that "local variable may not have been initialized".

int i,z;
if (st1.nextToken() == "Ace")
{
    String Ace = "14";
    i = Integer.parseInt(Ace);  
}
else if (st2.nextToken() == "Ace")
{
    String Ace = "14";
    z = Integer.parseInt(Ace);
}
else if (st1.nextToken() == "King")
{
    String King = "13";
    i = Integer.parseInt(King); 
}
else if (st2.nextToken() == "King")
{
    String King = "13";
    z = Integer.parseInt(King); 
}
else if (st1.nextToken() == "Queen")
{
    String Queen= "12";
    i = Integer.parseInt(Queen);
}
else if (st2.nextToken() == "Queen")
{
    String Queen= "12";
    z = Integer.parseInt(Queen);
}
else if (st1.nextToken() == "Jack")
{
    String Jack = "11";
    i = Integer.parseInt(Jack);
}
else if (st2.nextToken() == "Jack")
{
    String Jack = "11";
    z = Integer.parseInt(Jack); 
    break;
}
else
{
    i = Integer.parseInt(st1.nextToken());  
    z = Integer.parseInt(st2.nextToken());
}
if (i > z)
5
  • Which variable need initialized Commented Jan 27, 2016 at 3:06
  • 2
    What happens if first if condition (=="ACE") doesn't satisfy? local variable should have initiated in all possible execution paths before you use it. One solution just add else for first if and initiate i,z. I see another problem in your code, Java string comparison uses equals() NOT == Commented Jan 27, 2016 at 3:08
  • 1
    You need to have both i and z initialized in every path. Most of your conditional statements only initialize one of them. Also consider reformatting the code, since at first glance it looks like you are using nested conditionals. Commented Jan 27, 2016 at 3:16
  • Again in java, I believe you should use the .equals method for String comparison...not == operator....except you REALLY know what you are doing. as in if(st1.nextToken().equals("Ace")) Commented Jan 27, 2016 at 3:19
  • @Nambari and @gbenroscience are correct, you should be using st1.nextToken().equals("Ace") (and so on) for string comparisons. Commented Jan 27, 2016 at 3:27

4 Answers 4

2

Replace:

int i,z;

with:

int i=0, z=0;  //or whatever value you want

Or initialize both i and z to some value in all of your if-else blocks.

You cannot do if(i>z) unless both are initialized because you need to initialize local variables.

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

Comments

1

The compiler must be satisfied that both i and z have been initialised before it will permit the comparison i > z. Your code does not guarantee this as only some paths through your if..else logic will initialise both variables.

You have a couple of options:

  • Initiliase i and z at the very beginning, eg. to 0 or some other suitable default
  • Make sure you set i and z in every if block

A couple of other style issues:

  • Use equals() to compare strings, not ==, eg. st1.nextToken().equals("Ace")
  • Assigning a string value (containing a number) to a string and then parsing it for the integer is inefficient. You can simple assign an integer value.

Thus:

String Jack = "11";
i = Integer.parseInt(Jack);

can become:

i = 11;

or:

i = JACK;   // where JACK is a constant set to 11

You may also have a bug with respect to the tokens being read. Each if expression calls nextToken() which will cause the next token in the stream to be used. I imagine you want to compare the current token in each expression. If this is the case, then assign to two temporary variables to hold the next tokens for st1 and st2 before your if..else logic and compare these to Ace, King, etc.

Comments

0

A more elegant solution for String compares, in this case, would be to use a switch/case statement

Comments

0

I suggest using Integer class. If none of the conditions before last if statement satisfy, put a check if those Integers are initialized with null check.

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.