Reputation: 41
So when i go to compile my lab it says that PrintSum(num)
the num
is not initialized, but it shouldn't be because i'm using num
to call a method. this is part of my program, if you can tell me what it's saying that, that would be great. I'm sure it's an easy fix and im just thinking too much into it.
if(fileOpened&&inputFile.hasNext()){
while(inputFile.hasNext()){
if(inputFile.hasNextInt()){
PrintSum(num);
System.out.println("The sum of digits is " +PrintSum(num));
}
else
inputFile.next();
}
}
}
//method to print sum of 2 digits
public static int PrintSum(int number){
int result=0;
while(number!=0){
result=result+(number%10);
number=number/10;
}
return result;
}
Upvotes: 0
Views: 33
Reputation: 68715
If num
is a method local variable then it should be initialized before using. That is a java coding rule.
Upvotes: 2