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:
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?
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);
}