0

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;
  }
3
  • 1
    If num is a local variable then it should be intialized before using. That is a java coding rule. Commented Nov 8, 2013 at 3:12
  • 1
    Instead of int num; use int num = 0; (num must have a value before any access to its value occurs). Commented Nov 8, 2013 at 3:14
  • Unrelated. In Java, method names by convention should start with lower case letter. Read this: Code Conventions for the Java Programming Language Commented Nov 8, 2013 at 3:21

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.