Hello! For my computer science class, I have to convert a program with global variables to a program with all instantiated variables. I tried... and, well, the program works. Java gets it! But, I do have 24 errors.
Here is my new code:
//Note: You need to change the input file to the one on your computer! import java.io.*; import java.util.StringTokenizer; import java.util.HashSet; public class VowlezRUs { //method variables private static FileInputStream inFile; private static InputStreamReader inReader; private static BufferedReader reader; public static void main (String args[]) throws IOException { //Methods initFile(); doStuff(); inFile.close(); } public static void initFile() throws IOException { //Data Input inFile = new FileInputStream ("D:\\My Documents...\\blooby.txt"); inReader = new InputStreamReader (inFile); reader = new BufferedReader (inReader); } public static void doStuff() throws IOException { int leftmostIndex; //Line can't be null for loop to execute String line = "start"; while (line != null) { //Data conversion into strings line = reader.readLine(); if (line == null) { break; } StringTokenizer strTkn; strTkn = new StringTokenizer(line); String word = strTkn.nextToken(); String suffix = strTkn.nextToken(); //TEST //System.out.print(word); //add vowels to set for comparing HashSet vowels = null; vowels = new HashSet(); vowels.add("A"); vowels.add("C"); vowels.add("S"); vowels.add("L"); //TEST //System.out.println(word); //Set output variables... String wordPlural = word; String wordSuffix = word; //reverse word for testing for (int i = 0; i < word.length(); i++) { String wordR = word.substring(i, i+1) + wordR; } //reverse suffix for testing for (int i = 0; i < suffix.length(); i++) { String suffixR = suffix.substring(i, i+1) + suffixR; } //TEST //System.out.println(wordR); //if wordR ends in a consonant if (vowels.contains(wordR.substring(0,1)) == false) { //if wordR ends in more than one consonant if (vowels.contains(wordR.substring(1,2)) == false) { //Double the last letter, then add ‘H’ String lastLetter = wordR.substring(0,1); String wordPlural = wordR.concat(lastLetter); wordPlural = wordR.concat("H"); System.out.print("plural: " + wordR); //if suffix ends in a consonant if (vowels.contains(suffixR.substring(1,2)) == false) { //drop the leftmost letter of the final sequence of either vowels or consonants, then add the suffix String vowelSeries = word.substring(word.length()-leftmostIndex, word.length()); vowelSeries = vowelSeries.substring(1, vowelSeries.length()); String wordSuffix = wordSuffix.substring(0, leftmostIndex); wordSuffix = wordSuffix.concat(vowelSeries); wordSuffix = wordSuffix.concat(suffix); System.out.println("Suffix: " + wordSuffix); //if suffix ends in vowel }else{ //add the first letter of the suffix, then add the suffix String firstLetter = suffix.substring(0,1); wordSuffix = suffix.concat(firstLetter).concat(word); System.out.println("Suffix: " + wordSuffix); } //if wordR ends in 1 consonant }else{ //original string System.out.println("Original String: " + line); //add “GH” wordPlural = word.concat("GH"); System.out.println("Plural: " + wordPlural); //add the suffix wordSuffix = word.concat(suffix); System.out.println("Suffix: " + wordSuffix); } //if wordR ends in a vowel }else{ //if wordR ends in more than 1 vowel if (vowels.contains(wordR.substring(1,2)) == true) { //Index of leftmost of vowel series for (int i = 0; i < wordR.length(); i++) { if (vowels.contains(wordR.substring(i, i+1))) { leftmostIndex++; }else{ break; } } //original String System.out.println("Original String: " + line); //double the last letter, then add ‘H’ lastLetter = wordR.substring(0,1); wordPlural = word; wordPlural = wordPlural.concat(lastLetter); wordPlural = wordPlural.concat("H"); System.out.println("Plural: " + wordPlural); //if suffix ends in consonant if (vowels.contains(suffixR.substring(1,2)) == false) { //drop the leftmost letter of the final sequence of either vowels or consonants, then add the suffix vowelSeries = word.substring(word.length()-leftmostIndex, word.length()); vowelSeries = vowelSeries.substring(1, vowelSeries.length()); wordSuffix = wordSuffix.substring(0, leftmostIndex); wordSuffix = wordSuffix.concat(vowelSeries); wordSuffix = wordSuffix.concat(suffix); System.out.println("Suffix: " + wordSuffix); //if suffix ends in vowel }else{ //add the first letter of the suffix, then add the suffix firstLetter = suffix.substring(0,1); wordSuffix = suffix.concat(firstLetter).concat(word); System.out.println("Suffix: " + wordSuffix); } }else{ System.out.println("Original String: " + line); //drop the final vowel and add ‘G’ wordPlural = word.substring(0, word.length()-1); wordPlural = wordPlural.concat("G"); System.out.println("Plural: " + wordPlural); //if suffix ends in consonant if (vowels.contains(suffixR.substring(1,2)) == false) { //add the first letter of the suffix and then add the suffix firstLetter = suffix.substring(0,1); wordSuffix = wordSuffix.concat(firstLetter); wordSuffix = wordSuffix.concat(suffix); System.out.println("Suffix: " + wordSuffix); //if suffix ends in vowel }else{ //drop the first letter of the suffix and add the rest of the suffix suffix = suffix.substring(1, suffix.length()); wordSuffix = word; wordSuffix = word.concat(suffix); System.out.println("Suffix: " + wordSuffix); } } } //line } //massive while loop } //doStuff() } //class VowelsRU
And here is the original code:
/*-------------------------------- Name: Caitlin Connerney School: St. George's School Course: AP Computer Science Project: Vowels R Us ---------------------------------*/ //Note: You need to change the input file to the one on your computer! import java.io.*; import java.util.StringTokenizer; import java.util.HashSet; public class VowelsRUs { //method variables private static FileInputStream inFile; private static InputStreamReader inReader; private static BufferedReader reader; private static StringTokenizer strTkn; //string variables private static String line, suffix, suffixR, word, wordR, wordPlural, wordSuffix; private static String firstLetter, lastLetter, testLetter, vowelSeries, consonantSeries; //number variables private static int leftmostIndex; //set variables private static HashSet vowels = null; public static void main (String args[]) throws IOException { //Methods initFile(); doStuff(); inFile.close(); } public static void initFile() throws IOException { //Data Input inFile = new FileInputStream ("D:\\My Documents\\Senior\\APCompsci\\VHS APCS\\Data\\blooby.txt"); inReader = new InputStreamReader (inFile); reader = new BufferedReader (inReader); } public static void doStuff() throws IOException { //Line can't be null for loop to execute line = "start"; while (line != null) { //Data conversion into strings line = reader.readLine(); if (line == null) { break; } strTkn = new StringTokenizer(line); word = strTkn.nextToken(); suffix = strTkn.nextToken(); //TEST //System.out.print(word); //add vowels to set for comparing vowels = new HashSet(); vowels.add("A"); vowels.add("C"); vowels.add("S"); vowels.add("L"); //TEST //System.out.println(word); //Set output variables... wordPlural = word; wordSuffix = word; //reverse word for testing for (int i = 0; i < word.length(); i++) { wordR = word.substring(i, i+1) + wordR; } //reverse suffix for testing for (int i = 0; i < suffix.length(); i++) { suffixR = suffix.substring(i, i+1) + suffixR; } //TEST //System.out.println(wordR); //if wordR ends in a consonant if (vowels.contains(wordR.substring(0,1)) == false) { //TEST //System.out.print("c "); //if wordR ends in more than one consonant if (vowels.contains(wordR.substring(1,2)) == false) { //TEST //System.out.print("2c "); //Double the last letter, then add ‘H’ lastLetter = wordR.substring(0,1); wordPlural = wordR.concat(lastLetter); wordPlural = wordR.concat("H"); System.out.print("plural: " + wordR); //if suffix ends in a consonant if (vowels.contains(suffixR.substring(1,2)) == false) { //drop the leftmost letter of the final sequence of either vowels or consonants, then add the suffix vowelSeries = word.substring(word.length()-leftmostIndex, word.length()); vowelSeries = vowelSeries.substring(1, vowelSeries.length()); wordSuffix = wordSuffix.substring(0, leftmostIndex); wordSuffix = wordSuffix.concat(vowelSeries); wordSuffix = wordSuffix.concat(suffix); System.out.println("Suffix: " + wordSuffix); //if suffix ends in vowel }else{ //add the first letter of the suffix, then add the suffix firstLetter = suffix.substring(0,1); wordSuffix = suffix.concat(firstLetter).concat(word); System.out.println("Suffix: " + wordSuffix); } //if wordR ends in 1 consonant }else{ //TEST //System.out.print("1c "); //original string System.out.println("Original String: " + line); //add “GH” wordPlural = word.concat("GH"); System.out.println("Plural: " + wordPlural); //add the suffix wordSuffix = word.concat(suffix); System.out.println("Suffix: " + wordSuffix); } //if wordR ends in a vowel }else{ //if wordR ends in more than 1 vowel if (vowels.contains(wordR.substring(1,2)) == true) { //TEST //System.out.println("2v "); //Index of leftmost of vowel series for (int i = 0; i < wordR.length(); i++) { if (vowels.contains(wordR.substring(i, i+1))) { leftmostIndex++; }else{ break; } } //test //System.out.println(leftmostIndex); //original String System.out.println("Original String: " + line); //double the last letter, then add ‘H’ lastLetter = wordR.substring(0,1); wordPlural = word; wordPlural = wordPlural.concat(lastLetter); wordPlural = wordPlural.concat("H"); System.out.println("Plural: " + wordPlural); //if suffix ends in consonant if (vowels.contains(suffixR.substring(1,2)) == false) { //drop the leftmost letter of the final sequence of either vowels or consonants, then add the suffix vowelSeries = word.substring(word.length()-leftmostIndex, word.length()); vowelSeries = vowelSeries.substring(1, vowelSeries.length()); wordSuffix = wordSuffix.substring(0, leftmostIndex); wordSuffix = wordSuffix.concat(vowelSeries); wordSuffix = wordSuffix.concat(suffix); System.out.println("Suffix: " + wordSuffix); //if suffix ends in vowel }else{ //add the first letter of the suffix, then add the suffix firstLetter = suffix.substring(0,1); wordSuffix = suffix.concat(firstLetter).concat(word); System.out.println("Suffix: " + wordSuffix); } }else{ //TEST //System.out.println("v "); System.out.println("Original String: " + line); //drop the final vowel and add ‘G’ wordPlural = word.substring(0, word.length()-1); wordPlural = wordPlural.concat("G"); System.out.println("Plural: " + wordPlural); //if suffix ends in consonant if (vowels.contains(suffixR.substring(1,2)) == false) { //add the first letter of the suffix and then add the suffix firstLetter = suffix.substring(0,1); wordSuffix = wordSuffix.concat(firstLetter); wordSuffix = wordSuffix.concat(suffix); System.out.println("Suffix: " + wordSuffix); //if suffix ends in vowel }else{ //drop the first letter of the suffix and add the rest of the suffix suffix = suffix.substring(1, suffix.length()); wordSuffix = word; wordSuffix = word.concat(suffix); System.out.println("Suffix: " + wordSuffix); } } } //line } //massive while loop } //doStuff() } //class VowelsRU
So you can see the string variables and such that I tried to incorporate into the new code, but I can't figure out what else I can do to get rid of the errors. Here is my error list, should you also require that to assist me:
--------------------Configuration: VowlezRUs - JDK version 1.7.0_02 <Default> - <Default>-------------------- D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:88: error: cannot find symbol if (vowels.contains(wordR.substring(0,1)) == false) { ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:91: error: cannot find symbol if (vowels.contains(wordR.substring(1,2)) == false) { ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:93: error: cannot find symbol String lastLetter = wordR.substring(0,1); ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:94: error: wordPlural is already defined in doStuff() String wordPlural = wordR.concat(lastLetter); ^ D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:94: error: cannot find symbol String wordPlural = wordR.concat(lastLetter); ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:95: error: cannot find symbol wordPlural = wordR.concat("H"); ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:96: error: cannot find symbol System.out.print("plural: " + wordR); ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:99: error: cannot find symbol if (vowels.contains(suffixR.substring(1,2)) == false) { ^ symbol: variable suffixR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:103: error: wordSuffix is already defined in doStuff() String wordSuffix = wordSuffix.substring(0, leftmostIndex); ^ D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:132: error: cannot find symbol if (vowels.contains(wordR.substring(1,2)) == true) { ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:134: error: cannot find symbol for (int i = 0; i < wordR.length(); i++) ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:136: error: cannot find symbol if (vowels.contains(wordR.substring(i, i+1))) ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:148: error: cannot find symbol lastLetter = wordR.substring(0,1); ^ symbol: variable lastLetter location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:148: error: cannot find symbol lastLetter = wordR.substring(0,1); ^ symbol: variable wordR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:150: error: cannot find symbol wordPlural = wordPlural.concat(lastLetter); ^ symbol: variable lastLetter location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:155: error: cannot find symbol if (vowels.contains(suffixR.substring(1,2)) == false) { ^ symbol: variable suffixR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:157: error: cannot find symbol vowelSeries = word.substring(word.length()-leftmostIndex, word.length()); ^ symbol: variable vowelSeries location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:158: error: cannot find symbol vowelSeries = vowelSeries.substring(1, vowelSeries.length()); ^ symbol: variable vowelSeries location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:158: error: cannot find symbol vowelSeries = vowelSeries.substring(1, vowelSeries.length()); ^ symbol: variable vowelSeries location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:158: error: cannot find symbol vowelSeries = vowelSeries.substring(1, vowelSeries.length()); ^ symbol: variable vowelSeries location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:160: error: cannot find symbol wordSuffix = wordSuffix.concat(vowelSeries); ^ symbol: variable vowelSeries location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:166: error: cannot find symbol firstLetter = suffix.substring(0,1); ^ symbol: variable firstLetter location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:167: error: cannot find symbol wordSuffix = suffix.concat(firstLetter).concat(word); ^ symbol: variable firstLetter location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:179: error: cannot find symbol if (vowels.contains(suffixR.substring(1,2)) == false) { ^ symbol: variable suffixR location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:181: error: cannot find symbol firstLetter = suffix.substring(0,1); ^ symbol: variable firstLetter location: class VowlezRUs D:\My Documents\JCreator LE\MyProjects\VowlezRUs\src\VowlezRUs.java:182: error: cannot find symbol wordSuffix = wordSuffix.concat(firstLetter); ^ symbol: variable firstLetter location: class VowlezRUs 26 errors Process completed.
Also, here is what consists of the text file I'm using for the input stream:
XQAC ZVM PDAE AV SNIC SY AAAB BB
Essentially, I am given a fictional word, and need to add a fictional suffix to the end. My main problem with the program, as you can see, is that I am having trouble declaring variables properly. If you would like the assignment prompt, please say so, as I would be happy to offer any documents that would help you help me!
Thank you in advance!