0

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?

Hydraxia
  • 81
  • 2
  • 9
  • 1
    Add a parameter to your shortAnalyze method if you want to use in there. – ControlAltDel Feb 04 '15 at 18:30
  • Hello ControlAltDel, thank you for your answer. Sorry, I'm a bit confused. If I added a parameter to my method, it would stop being a void? And that means I need to return a value, but I only want to write out 'nAll', I don't want to change it's value, return it and use somewhere else. I feel ashamed, but I don't understand you, sorry. – Hydraxia Feb 04 '15 at 18:43

1 Answers1

0

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);

       }
User31689
  • 716
  • 1
  • 6
  • 12
  • Thank you GobletSky, this really helped! I created a variable ''static int nAll''; And I also noticed that with creating ''public static int nAll'' I'm able to reach ''nAll'' everywhere in my package, you solved 2 of my problems with 1 answer! Thank you again, sir! – Hydraxia Feb 04 '15 at 19:20
  • You are welcome. Though I recommend you think carefully whether you want to use instance variables or local variables. Check this answer : http://stackoverflow.com/a/4588772/1559914 – User31689 Feb 04 '15 at 19:39