Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Changing global variables to local?

    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?

    Code:
     
    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
          }
        }
    }

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You would be more likely to get an answer to this in a Java forum.

    Perhaps a moderator will move it for you.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,298
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    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!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •