0

I'm writing a program for an assignment which I have to convert english to Klingon and I'm pretty sure all my code works except for the if statement in my method transToKlingon. I cannot figure out why the if statement never is true.

import java.util.*;
import java.io.*;

class transToKling
{
    public static void main(String[] args) 
    throws java.io.IOException    
{        
      // Create array  
      String[][] klingTrans = new String[16][2];      
      String filName = " ";        

      filName = "klingonTranslate.txt";        
      Scanner input = new Scanner(new File(filName));        
      for (int row = 0; row < klingTrans.length; row++)        
      {            
           for (int col = 0; col < klingTrans[row].length; col++)                
                klingTrans[row][col] = input.nextLine();        
      }        
     input.close();  


     Scanner sc = new Scanner(System.in);
     System.out.print("Enter english phrase to translate to Klingon: ");
     String phrase = sc.nextLine();

     transToKlingon(phrase, klingTrans);
}

public static void transToKlingon(String txt, String[][] klingon) 
{
    // split txt string and find search array for match.    
    String[] s = txt.split(" ");

    for (String txtSplit : s) 
    {
        for (int i = 0; i < klingon.length; i++) 
        {                                                               
            if (klingon[i][0] == txtSplit) 
            {                   
                System.out.print(klingon[i][1] + " ");
            }
        } // end i for
    } // end for each txtSplit
} // end transToKlingon Method  
}
  • 1
    Don't compare Strings with `==` or `!=`. Understand that these operators check for *reference* equality, that the two String variables refer to the exact same ***String object***, and that's not what you want. Instead you want to test for *functional* equality -- that the two Strings have the same letters in the same order. For that use the `equals` or `equalsIgnoreCase` methods. – Hovercraft Full Of Eels Mar 31 '17 at 21:41
  • Thank you for your help. I've been beating my head over this for awhile now, much appreciated. – YachtRocker Mar 31 '17 at 21:53

0 Answers0