my dog learned polymorphism*
The moose likes Java in General and the fly likes Undefined Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Undefined Variables" Watch "Undefined Variables" New topic
Author

Undefined Variables

Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
I'm writing a function that will create a 10-digit ID for a file based on its name. Since I'm using the String.hashCode() function to start with, sometimes I get a string that's less than the desired 10 digits.
To correct this, I'm making that string into a char array, checking its length, and padding with "x" if necessary. It works great, except for one thing: even after I've created the new, 10-place array, my compiler thinks it's nonexistent and gives me errors.
Here is the exact code:
public String makeID(String y, int place)
{
int code = y.hashCode();
if (code <= 0)
{
code = java.lang.Math.abs(code);
code *= 2;
}
code = java.lang.Math.abs(code);
String id = java.lang.Integer.toString(code);
String copy = id;
char idChars[] = copy.toCharArray();
if (idChars.length < 10)
{
char[] fullidChars = new char[10];
for (int a=0;a<idChars.length;a++)>
{
fullidChars[a] = idChars[a];
}
for (int b=idChars.length;b<10;b++)
{
fullidChars[b] = 'x';
}
}
else
{
char[]fullidChars = new char[10];
for (int c=0;c<10;c++)
{
fullidChars[c] = idChars[c];
}
}
String finalID = new String(fullidChars);
return finalID;
}
When I compile, I get this:
./filelist.java:63: Undefined variable: fullidChars
String finalID = new String(fullidChars);
^
Any idea why it would do that? fullidChars is clearly defined, whether or not I get a 10-digit string returned by hashCode().
Thanks,
Alex Kirk
Tom McComb
Ranch Hand

Joined: Jun 07, 2001
Posts: 30
Have you tried this?
StringBuffer sb = new StringBuffer( idString );
while (sb.length() < 10 ) {
sb.append( "x" );
}
String newFileName = sb.toString();
This may save you some headaches.
------------------
Tom McComb


Tom McComb
Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
Awesome! That worked like a charm. I guess I just need to continue familiarizing myself with all of the data types, etc. out there in Java.
Thanks!
Alex
Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54
Yes, it is much cleaner to use the String class that Java provides than using an array of characters. However, while replacing the character array with a String solved the problem, it doesn't explain why the problem occurred; since fullidChars is declared inside either the if or else block, it has limited scope and isn't seen outside of the block.
So, while it is much better to use String, you *could* have just declared fullidChars outside of the block.


SCJP, SCWCD
Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
I actually found out about that yesterday while working on another section of the program.
Is there any way to expand the scope of a variable outside of a block by an explicit command? It'd be really frustrating to have to always have to declare my variables outside of blocks, considering some of the algorithms I have in mind for this program.
Alex
Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54
Moving to Java from C/C++? You may be able to do that in other languages, but I'm not familiar with such a feature in Java. It seems like it would go against Java's purpose of making code easier to understand; you see a variable being used but can't see where it was declared because it was inside of a block.
Jason
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Even if you could do that, it would be considered bad form to do so. Check out the Chicken Coop here at the Ranch for a little help on style in Java programming. There is a specific spot that should contain your declarations. Always putting them there will make your code more readable and much easier to maintain for the programmer after you. Imagine trying to hunt down the bug that you described in 10,000 lines of code! It could take some time and headaches.
Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
Well then, let me ask you a couple of questions about how Java allocates memory.
When I declare a variable but don't set it equal to something, i.e. "String myStr = null", is memory allocated? If so, how much?
Also, if I create an array -- say, "String myArray[1000]" -- does Java reserve memory for each of those array slots, or does it simply allocate memory for the array itself? In other words, do unused array slots take up memory? This is of particular concern to me after finding out that Java really doesn't dynamically resize arrays.
Alex
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
When you declare "String myStr = null", you are creating a reference to a String object, but the memory for the String is not allocated until you actually create the object itself.
The same thing applies to arrays. Also, you don't have to specify a size when you declare your array. You can just use "String myArray[]", then give a size when you initialize the array, "myArray = new String[1000]".
Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54

This is of particular concern to me after finding out that Java really doesn't dynamically resize arrays.

Are you coming from a C/C++ background? I would guess so, based on your use of a character array in place of a String, though I could be mistaken. In any case, arrays are typically fixed-size in programming languages. If you want a dynamic array, you need to use an ADT like a Vector or an ArrayList.
Jason
Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
Yes, I am coming from a C/C++ background. Honestly, though, I don't really have too hard-and-fast of a programming style yet, as I'm still dabbling with C++, Perl, JavaScript, and even the occasional (yuck!) VB.
As far as the sizing of arrays goes, though, here is an example of why I'm interested in resizing them (please let me know if Vectors, etc. would make more sense):
In creating a list of available files for a filesharing program, I'd like to go through multiple directories. Thus, I'll have an array of one size after the first directory, which will then grow after I read another directory.
Alex
Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54
If you know for a fact that the directory will contain no more than 1000 files, you could create an array of 1000 elements. However, if you exceed this number, you'll get an
ArrayIndexOutOfBoundsException. Your array will never grow to accommodate additional elements. If you use an array with a ridiculously large capacity, you will be wasting the space you're not using, since the space for the array is set aside when you use the new operator.
In any case, it seems pretty risky to assume you will know the maximum number of files. So, you'll probably want to use a growable array. Check the java.util package for info on Vector and ArrayList. You'll probably want to use ArrayList; there are some issues with Vector.
From the Java API:

The Enumerations returned by Vector's elements method are not fail-fast.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Undefined Variables
 
Similar Threads
Declared Variables Not Seen By Compiler
Variable Scope
Passing Array Elements to Method
Guidelines for 'extenders' of a class
ascii to binary conversion