0

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!

Simon Hillary
  • 213
  • 1
  • 4
  • 12

2 Answers2

7

Java passes references by value. So this line:

wordI = wordI + words[0];

assigns a new String to wordI but it is only the local (within the method) copy of the reference which changes. The original word1 variable still refers to the original String.

To make it work, you could use an array instead:

public static void synToWords(String syn, String[] word1And2){
    String[] words = syn.split("\\t");
    word1And2[0] = word1And2[0] + words[0];
    word1And2[1] = word1And2[1] + words[1];
}

and in your main code:

String[] words = { "", "" };
synToWords(synFR.getLine(3), words);
System.out.println(words[0] +" " + words[1]);

Or you could simply return a array of String:

public static String[] synToWords(String syn){
    String[] words = syn.split("\\t");
    return new String[] {words[0], words[1]};
}

String[] words = synToWords(synFR.getLine(3));
System.out.println(words[0] +" " + words[1]);
Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
1

Java is entirely call-by-value. wordI and wordII are copies of the values of two reference expressions. A reference expression is either null or a pointer to an object. When you assign to e.g. wordI in synToWords you are only changing what synToWords' local copy points to.

One solution would be to have the method return a String[] containing the two pieces. The caller could then assign elements of the result to its variables.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75