I have a void method which is supposed to take a String and split it into two strings based on a delimiter. Having used the debugger the method seems to be working correctly, but the two empty strings I pass in for the method to store the results don’t seem to be getting updated. I must be doing something idiotic here but any help is appreciated!
public static void main(String[] args) {
String dictFile = "/Users/simonrhillary/Desktop/Dictionary(3).txt";
String synFile = "/Users/simonrhillary/Desktop/Synonyms(2).txt";
fileReader dictFR = new fileReader();
fileReader synFR = new fileReader();
dictFR.filePath = dictFile;
synFR.filePath = synFile;
dictFR.openFile(dictFile);
synFR.openFile(synFile);
String[] dictionary = dictFR.fileToArray();
String[] synonyms = synFR.fileToArray();
String regx = "^[aflmptu]+$";
String regexTemplate = "^[]+$";
String word1 = "";
String word2 = "";
synToWords(synFR.getLine(3), word1, word2);//error seems to be here.
//word1 and word 2 stay ""
System.out.println(word1 +" " + word2);
printArray(findCombos(dictionary, word1, word2));
}
public static void synToWords(String syn, String wordI, String wordII){
String[] words = syn.split("\\t");
wordI = wordI + words[0];
wordII = wordII + words[1];
}
there are other methods I haven’t posted but they are all working fine. It’s just the synToWords method thats the problem. Many Thanks!
Java passes references by value. So this line:
assigns a new String to
wordIbut it is only the local (within the method) copy of the reference which changes. The originalword1variable still refers to the original String.To make it work, you could use an array instead:
and in your main code:
Or you could simply return a array of String: