Jump to content

Local variable is never read

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
corsica

corsica

    Newbie

  • Members
  • Pip
  • 3 posts
Hello all,

I have a simple question regarding declare of veriables.
I have the following task:
if (type == "xx")
{
String[] yy = {"a", "b", "c"}; <--here I have a message(Local variable is never read)
}
else
{
String[] yy = {"d", "e", "f"};<--here I have a message(Local variable is never read)
}

So I need to declare an array depending on "type" value, but my arrays are not seen for future using.
Please help me with this question.

Thanks in advance!

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Declare the array outside the if-else like
String[] yy;

if (type == "xx")
    yy = {"a", "b", "c"}; 
else
  yy = {"d", "e", "f"};

Variables only exist within the { } where they are declared in

#3
corsica

corsica

    Newbie

  • Members
  • Pip
  • 3 posts
Yes, I've tried such way, but I have the following error:
String[] yy;
if (type == "xx")
yy = {"a", "b", "c"}; <--Array constants can only be used in initializers
else
yy = {"d", "e", "f"};<--Error: Array constants can only be used in initializers


#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Oops, my bad :)

String[] yy;

if (type == "xx")
    yy = new String[]  {"a", "b", "c"}; 
else
  yy = new String[]  {"d", "e", "f"};


#5
corsica

corsica

    Newbie

  • Members
  • Pip
  • 3 posts
Thank you very much!

My problem is solved:)