Claudia Gomes
Claudia Gomes

Reputation: 41

Compiling error when reading array from file

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

Answers (1)

Juned Ahsan
Juned Ahsan

Reputation: 68715

If num is a method local variable then it should be initialized before using. That is a java coding rule.

Upvotes: 2

Related Questions