当前位置: 动力学知识库 > 问答 > 编程问答 >

Java: Searching a 2D array for Strings and displaying the data

问题描述:

I am creating a program which acts as a translator for given words. I have created a text file with the data I am using, reading that into a 2D array (English in column 0, translation in column 1, 16 rows in total). I prompt the user to enter a string and pass that string, the 2D, and a blank String to hold the translation to my translation method (named: turnKlingon). I am using the String tokenizer to pick out specific words. My problem is that I cannot figure out how to search my 2D array in column 0 for the English and only print the column 1 translated word.

import java.util.*;

import java.io.*;

public class project9

{

public static void main(String[] args)

throws java.io.IOException

{

System.out.println("Klingon Translator");

System.out.println();

System.out.println();

String userString = " ";

userString = loadUserString();

String[][] translate = new String[16][2];

loadTranslateString(translate);

String Klingon = " ";

turnKlingon(Klingon, userString, translate);

}

public static String loadUserString()

{

Scanner input = new Scanner(System.in);

String s1 = " ";

System.out.println("Please enter a sentence that you would like translated to Klingon: ");

s1 = input.nextLine().trim().toUpperCase();

return s1;

}

public static void loadTranslateString(String[][] translate)

throws java.io.IOException

{

String filName = " ";

filName = "C:\\tmp\\transKling.txt";

Scanner input = new Scanner(new File(filName));

for (int row = 0; row < translate.length; row++)

{

for (int col = 0; col < translate[row].length; col++)

translate[row][col] = input.nextLine();

}

input.close();

}

public static void turnKlingon(String Klingon, String userString, String[][] translate)

{

String userStringPassed = userString;

String[][] translatePassed = translate;

String s2 = " ";

StringTokenizer st = new StringTokenizer(userStringPassed);

int numberOfWords = st.countTokens();

System.out.println("Number of Tokens: "+ numberOfWords);

int counter = 1;

while (counter <= numberOfWords)

{

s2 = st.nextToken(); //string tokenizer

System.out.print(s2 + "_"); //string tokenizer

for(int r = 0; r < translate.length; r++)

{

for(int c = 0; c < translate[r].length; c++)

{

if (translate[r][c].compareTo(s2) == 0)

{

translate[0] = translate[1];

}

}

System.out.println(translate[0] + "," + translate[1]);

counter++; //string tokenizer

}//end while

}

}

}

网友答案:

After messing around with this code for a few hours I finally managed to solve my own problem. I also added a while true loop in the main method to allow the user to translating multiple times as well as an option to translate to another language (German) though I have omitted that method from this reply.

import java.util.*;
import java.io.*;
public class project9
{
    public static void main(String[] args)
    throws java.io.IOException 
    {
        Scanner input = new Scanner(System.in);

        System.out.println("English to Klingon/German Translator");
        System.out.println();
        System.out.println();

     while (true)
     {
        int again = 999;
        int language = 999;
        String userString = " ";
        userString = loadUserString();

        String[][] translate = new String[16][2];
        loadTranslateString(translate);

        System.out.println("Would you like to translate to Klingon or German? \n Press 1 for Klingon: \n Press 2 for German: ");
        language = input.nextInt();

        if (language == 1)
        {        
        turnKlingon(userString, translate);
        }
        else if (language == 2)
        {
        turnGerman(userString, translate);
        }
        else 
        {
            System.out.println("Invalid input");
        }

        System.out.println();
        System.out.println();
        System.out.println("Would you like to translate another sentence? \n  Press 1 for yes or 0 for no: ");
        again = input.nextInt();

        if (again == 0)
        {
            System.out.println("Goodbye!");
            break;
        }
        else if (again == 1)
        {
            continue;
        }

     }
    }

        public static String loadUserString()
        {
           Scanner input = new Scanner(System.in); 
           String s1 = " ";
           System.out.println("Please enter a sentence that you would like translated: ");
           s1 = input.nextLine().trim().toUpperCase();
           return s1;

    }

        public static void loadTranslateString(String[][] translate)
        throws java.io.IOException 
        {
          String filName = " ";       
          filName = "C:\\tmp\\transKling.txt";       
          Scanner input = new Scanner(new File(filName));       
          for (int row = 0; row < translate.length; row++)       
          {           
               for (int col = 0; col < translate[row].length; col++)               
                    translate[row][col] = input.nextLine();       
          }       
          input.close();    

    }

       public static void turnKlingon(String userString, String[][] translate)
       {
           String userStringPassed = userString;
           String[][] translatePassed = translate;
           String s2 = " ";
           StringTokenizer st = new StringTokenizer(userStringPassed);
           int numberOfWords = st.countTokens();
           System.out.println();
           System.out.println("Any words that cannot be directly \n translated will remain in English");
           System.out.println("____________________________________");
           System.out.println();
           System.out.print("Translation: ");
           int counter = 1;
           boolean found = false;
           int translateCounter = 0;


       while (counter <= numberOfWords)
       {
        s2 = st.nextToken();  //string tokenizer
         //string tokenizer



      for(int r = 0; r < 16; r++) 
      { 
          for(int c = 0; c < 18; c++) 
          {
              found = false;

              if (translateCounter == 16)
              {

                  System.out.print(s2+ " ");
                  translateCounter = 0;
                  break;
              }
              else if (translate[r][0].compareTo(s2) == 0)
              {
               found = true;  
              }
              else if (translate[r][0].compareTo(s2) != 0)
              {
                found = false;
                r++;
                c = -1;;
                translateCounter++;
                continue;

              }


            if (found == true)
            {

            System.out.print(translate[r][1]+ " ");
            translateCounter = 0;
            c = -1;
            r = -1;
            break;
           }


         }  

        counter++;//string tokenizer 


        break;
       }//end while

     }
   }
分享给朋友:
您可能感兴趣的文章:
随机阅读: