...

View Full Version : Changing global variables to local?



lafret
01-21-2012, 09:13 PM
I'm doing a little project with Java but I've run into some trouble. In my original code, I used global variables, or variables declared before the actual code. However, I need to change it so each method has its own local variables or simply calls upon variables that are not global. Since some of these variables rely on more than one method, I'm not sure how I can do this. I'm trying to use a boolean function, but I'm not getting anywhere. Can any one help?



import java.io.*;
import java.lang.*;
public class Vowels2 {

private static FileInputStream inFile;//all variables declared
private static InputStreamReader inReader;
private static BufferedReader reader;
private static char first, second, last, recent;
private static int length, wordLength, spaceIndex, cntr;
private static String line, word, suffix, plural, suffixed;

public static void main(String[] args) throws IOException {//methods listed
inFile();
endFile();
}

private static void inFile() throws IOException{//file is read
inFile = new FileInputStream("C:\\!!VHSAPCSData\\Vowels.txt");
inReader = new InputStreamReader(inFile);
reader = new BufferedReader(inReader);
}

private static void plural(){
wordLength = word.length();
last = word.charAt(wordLength-1);
second = word.charAt(wordLength-2);
if(((last=='A')||(last=='C')||(last=='S')||(last=='L')) && ((second!='A')&&(second!='C')&&(second!='S')&&(second!='L'))){
plural = word.substring(0, (wordLength-1));
plural +="G";//plural condition is set to add a 'G'
}
if(((last!='A')&&(last!='C')&&(last!='S')&&(last!='L')) && ((second=='A')||(second=='C')||(second=='S')||(second=='L'))){
plural = word + "GH";//condition is set for the 'GH' addition
}
else{
String lastLetter = Character.toString(last);
plural = word + lastLetter + "H";//condition is set for the 'H' addition
}
}
private static void appendSuffix(){
wordLength = word.length();
last = word.charAt(wordLength-1);
second = word.charAt(wordLength-2);
first = suffix.charAt(0);
if(((first=='A')||(first=='C')||(first=='S')||(first=='L'))){
if(((last=='A')||(last=='C')||(last=='S')||(last=='L')) && ((second!='A')&&(second!='C')&&(second!='S')&&(second!='L'))){
String append = suffix.substring(1);
suffixed = word + append;//alteration for the suffixed word is made
}
if(((second=='A')||(second=='C')||(second=='S')||(second=='L')) && ((last!='A')&&(last!='C')&&(last!='S')&&(last!='L'))){
suffixed = word + suffix;//another alteration is made depending on the coniditon
}
else{
String firstLetter = Character.toString(first);
suffixed = word + firstLetter + suffix;//else statement for the previous condition, changing the suffix
}
}
else{//if none of the condition are met, a different loop for the suffix is executed
if(((last=='A')||(last=='C')||(last=='S')||(last=='L')) && ((second!=('A'))&&(second!='C')&&(second!='S')&&(second!='L'))){
String firstLetter = Character.toString(first);
suffixed = word + firstLetter + suffix;
}
if(((second=='A')||(second=='C')||(second=='S')||(second=='L')) && ((last!=('A'))&&(last!='C')&&(last!='S')&&(last!='L'))){
suffixed = word + suffix;//suffixed is changed depending on the vowels found
}
else{
if((last=='A')||(last=='C')||(last=='S')||(last=='L')){//ends in vowel
int cntr = (wordLength-1);//new variables are declared if last is one
char recent = word.charAt(cntr);
while ((recent=='A')||(recent=='C')||(recent=='S')||(recent=='L')){
cntr--;
recent = word.charAt(cntr);
}
String part1 = word.substring(0, cntr+1);
String part2 = word.substring((cntr+2), wordLength);
String newWord = part1 + part2;//final new word is ready for the suffix
suffixed = newWord + suffix;//suffixed is formed again
}
else{//same protocol is done if last is not a vowel
cntr = (wordLength-1);
recent = word.charAt(cntr);
while ((recent!='A')||(recent!='C')||(recent!='S')||(recent!='L')){
cntr--;
recent = word.charAt(cntr);
}
String part1 = word.substring(0, cntr);
String part2 = word.substring((cntr+1), wordLength);
String newWord = part1 + part2;
suffixed = newWord + suffix;//another suffix is formed
}
}
}
}

private static void printResults(){//printing the final results
System.out.println("Line: " + line);
System.out.println("Plural: " + plural);
System.out.println("Suffix: " + suffixed);
System.out.println(" ");
}

private static void endFile() throws IOException{//a loop function is prepared
line = reader.readLine();
while(line!=null){//if the line is null, the code terminates
spaceIndex = line.indexOf(" ");
length = line.length();
word = line.substring(0, spaceIndex);
suffix = line.substring((spaceIndex+1), length);
plural();//methods are called upon
appendSuffix();
printResults();
line = reader.readLine();//variables are declared and the lext line is read
}
}
}

felgall
01-21-2012, 10:36 PM
You would be more likely to get an answer to this in a Java forum.

Perhaps a moderator will move it for you.

Philip M
01-22-2012, 09:11 AM
This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum