aspose file tools*
The moose likes Java in General and the fly likes String to integer conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to integer conversion" Watch "String to integer conversion" New topic
Author

String to integer conversion

Qamar Ahmad
Greenhorn

Joined: Jul 17, 2006
Posts: 1
I have a question about type conversion/arrays. It is relate to following code snippet:

String _today;
String [] _date;
if ( match != null ) {
if ( line.indexOf("disbursement") <= 0 ) {
int fromInd = line.indexOf("from ");
int throughInd = line.indexOf("through");
addToXML(match.StartDateKey, line.substring(fromInd + 5, throughInd));
_today = line.substring(fromInd + 5, throughInd);
_date = _today.split("/");
//System.out.println(_date[0] + " " + _date[1]);
}
} else {
//check if line starts with ( and find money value
if ( line.indexOf("@ $") > 0 ) {
int fromIndDaysTillEOM = line.indexOf("(");
int throughIndDaysTillEOM = line.indexOf(" days");
String daysTillEOM = line.substring(fromIndDaysTillEOM + 1, throughIndDaysTillEOM).trim();
int temp = Integer.parseInt(_date[1].trim()) + Integer.parseInt(daysTillEOM);
}

The error occurs in the last line. "varialbe _date might not have been initialized". Clearly I am initializing the array _date in the first line.

Thanks!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8713
    
    6

You are declaring _date on the second line. You are initalizing it on line 9. That is, if match is not null and line.indexOf("disbursement") <= 0. should match be null or "disbursement" not appear, _date will be uninitalized.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Alana Sparx
Ranch Hand

Joined: Feb 14, 2006
Posts: 121
Hi Quammar

You said
Clearly I am initializing the array _date in the first line

Actually, you're not! Your declaring it. You attempt to give the array a value using the split() method of the variable _today. However, you attempt to do this inside a nested if() block inside the
if(match != null)
block - what's going to happen if either of these if() conditions are not met?
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Technically, you are only declaring the array on the second line, not initializing it. And any time you declare a value in one scope, and don't initialize it until you enter a smaller scope (which is conditionally not executed), such as an if structure, while strucutre, for loop, switch structure, try/catch/finally block, etc, the compiler produces this warning.

Basically, this warning means this: While you (the programmer) may be certain that the conditions are sufficiently tight enough that the variable will always be initialized, the compiler is NOT. Hence, when you attempt to read that variable, the compiler cannot guarantee that there is valid data in that object.

This was an important design decision when creating the java compiler. In c++, the computer doesn't care what happens if you don't bother initializing a variable. It just assigns it to a location in memory, and the value of that object will simply be whatever value was in the memory location before. In java, however, one of the primary design goals was that the client was never exposed to the actual hardware of the machine. Thus, it cannot allow you to read the value that just so happened to already be there. So, there were two choices: Enforce initialization, or provide a default value of null. The latter is a poor idea, because when you attempt to read that value, there is a signification chance that you will throw a null pointer exception. The other solution, forcing the user to guarantee initialization first means that the programmer will provide whatever default is reasonable in any given situation. Furthermore, if null IS the appropriate default, then it will be explicit from the code.

EASY FIX:

An easy fix to this problem is to simply initialize it as null. When you do this, though, be VERY certain that the value will no longer be null the first time you attempt to access that value, or else check for a null value BEFORE you attempt to access that value for the first time (fail fast). This way, you don't get a null point exception from 12 levels down the stack trace, and not be sure about what ACTUALLY caused the null pointer exception.

e.g.



Special Case:

For String arrays, or in fact, for arrays in general, instead of initializing the values as null, you can initialize it as a zero-length array. This way, you will not get null pointer exceptions at all. Instead, when it attempts to iterate over the array, there will just be no values there to iterate over. There are some cases where this is justified (you know that it won't always get a value, and thats ok). However, this method is not "fail-fast." If you don't initialize the array, you might not find out until much later in the code, when something isn't working right, and you can't figure out why not.

Hope this helps!

- Adam
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Technically, you are only declaring the array on the second line, not initializing it. And any time you declare a value in one scope, and don't initialize it until you enter a smaller scope (which is conditionally not executed), such as an if structure, while strucutre, for loop, switch structure, try/catch/finally block, etc, the compiler produces this warning.

Basically, this warning means this: While you (the programmer) may be certain that the conditions are sufficiently tight enough that the variable will always be initialized, the compiler is NOT. Hence, when you attempt to read that variable, the compiler cannot guarantee that there is valid data in that object.

This was an important design decision when creating the java compiler. In c++, the computer doesn't care what happens if you don't bother initializing a variable. It just assigns it to a location in memory, and the value of that object will simply be whatever value was in the memory location before. In java, however, one of the primary design goals was that the client was never exposed to the actual hardware of the machine. Thus, it cannot allow you to read the value that just so happened to already be there. So, there were two choices: Enforce initialization, or provide a default value of null. The latter is a poor idea, because when you attempt to read that value, there is a signification chance that you will throw a null pointer exception. The other solution, forcing the user to guarantee initialization first means that the programmer will provide whatever default is reasonable in any given situation. Furthermore, if null IS the appropriate default, then it will be explicit from the code.

EASY FIX:

An easy fix to this problem is to simply initialize it as null. When you do this, though, be VERY certain that the value will no longer be null the first time you attempt to access that value, or else check for a null value BEFORE you attempt to access that value for the first time (fail fast). This way, you don't get a null point exception from 12 levels down the stack trace, and not be sure about what ACTUALLY caused the null pointer exception.

e.g.



Special Case:

For String arrays, or in fact, for arrays in general, instead of initializing the values as null, you can initialize it as a zero-length array. This way, you will not get null pointer exceptions at all. Instead, when it attempts to iterate over the array, there will just be no values there to iterate over. There are some cases where this is justified (you know that it won't always get a value, and thats ok). However, this method is not "fail-fast." If you don't initialize the array, you might not find out until much later in the code, when something isn't working right, and you can't figure out why not.

Hope this helps!

- Adam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to integer conversion
 
Similar Threads
constructor to add to an array list
HELP!
Arraylist
Arrays with files
creating a new object in an ArrayList