Search code examples
javaeclipseparametersvoidsubroutine

Call a void subroutine's variables in Java


This is my first question here, thank you for your help! ( Please excuse any grammar mistakes I make, I'm still learning English )

I've started studying this Java language around 2-3 weeks ago, so this question will be probably quite easy for you, but I don't understand my problem. I believe I've maden a small mistake somewhere in my code, losing many hours thinking and researching online forums. ( Maybe the research wasn't useless, but I still can't solve this alone, by myself. ) /I'm using Eclipse/

My program has 4 subroutines:

  1. public static void main(): This is where the 3 others are called. The purpose of the program is to ask the user for a valid file name, if it exists, the program reads it and counts the characters, numbers etc. within the file, row by row.
  2. static void shortAnalyze(): The user can decide between a short and a long analyze, this will only display the overall character count in the text, all the numbers, the number of rows.
  3. static void detailedAnalyze(): This is very similar to the previous one, except displays a lot more details, like special characters count, the longest word, the most used letters, vowels and consonants, number of capital letters etc. ( and maybe create a new empty text file and make a log )
  4. static void analyzeText(): This is the only subroutine that calculates, the others' function is only to write out the informations. Basically, the 2 subroutines, shortAnalyze() and detailedAnalyze() are 'mathematical equations' grouped together.

The current code isn't complete yet, but I can run it without errors. I have also used a Java class named TextIO, which I find easier to work with than Scanner.

This is where my actual problem begins:

static void analyzeText() {

        String   line;
        char     currChar    = ' ';
        int      nAll        = 0;          // nLetters    + nNumbers   + nOther    +nEmpty
        int      nCharacters = 0;          // nLetters    + nNumbers   + nOther
        int      nEmpty      = 0;          //                                       nEmpty
        int      nOther      = 0;          //                            nOther
        int      nNumbers    = 0;          //               nNumbers
        int      nLetters    = 0;          // nLetters
        int[]    uniqueChar  = new int[2048];
        boolean  readingFile = true;       // it is true, because analyzeText can be called only if fileName is valid

        while(readingFile){
            try {

                line = TextIO.getln();
                for (int i=0; i < line.length(); i++ ){
                        currChar = line.charAt(i);
                        if (currChar == ' '){
                            nEmpty++;
                            nAll++;
                        }

                } //end of for
            } //end of try
            catch (IllegalArgumentException end_of_file) {
                break;
            }



            } //end of analyzeText
       } //end of readingFile

( Please note that there are a lot of useless and functionless things and codes )

And this is my subroutine:

static void shortAnalyze(){

           System.out.println();

       }

I know ( or I'm totally wrong ) that a void cannot return any values. I still want something like:

static void shortAnalyze(){

           System.out.println(analyzeText.nAll);

       }

or

static void shortAnalyze(){

           System.out.println(nAll);

       }

So I want to call a variable from the void analyzeText() into another void, called shortAnalyze(). Do you think it's possible somehow? Do you have any tips for me, suggestions?


Solution

  • You can declare the variables that you want to share between methods as an instance variable of the class. Just declare them outside the method.
    Then you can use:

    static void shortAnalyze(){
    
               System.out.println(nAll);
    
           }