2013-04-06 1 views
1

이 프로그램이 있지만 몇 가지 문제가 있습니다 : 여기는 내가 계산할 곳입니다.정적 사용 Morsecode

import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 

public class MorseCode { 
public MorseCode() 
    { 
    }//end default constructor 
    public String[] translateHere(String s)throws IOException 
    { 
     String compare = s, codedLine = ""; //userInput toUpperCase 
     int length = compare.length(); //length of userInput 
     String line, file = "C:\\APCS Course B\\Unit 4\\Unit 4 Documents\\morse.txt";// variable holding file name and variable for each letter/number 
     char code; 
     //Constants 
     final int MAX = 36; 
     //Arrays 
     char[] morseLetter = new char[MAX]; 
     String[] morseCode = new String[MAX]; 
     String[] newMessage = new String[length]; 
     //putting user input in a character array; 
     char[] userLetters = compare.toCharArray(); 
     //object creation 
     File openFile = new File(file); 
     Scanner inFile = new Scanner(openFile); 
     int counter = 0; 
     while(inFile.hasNext()) 
      { 
       line = inFile.next(); 
       code = (char)line.charAt(0); 
       //System.out.println(code); 
       morseLetter[counter] = code; 
       morseCode[counter] = inFile.next(); 
       counter++; 
      }//end nested while loop 
     for(int j = 0; j < length; j++) 
     { 
      for(int k = 0; k < MAX; k++) 
      { 
       if(userLetters[j] == morseLetter[k]) 
       { 
        newMessage[j] = morseCode[k]; 
       } 
      }//end nested for loop 
     }//end for loop 
     return newMessage; 
    }//end method that completes translateion 
    public String toString(String a, String[] b) 
{ 
    System.out.println("Input: " + a); 
    System.out.println("Output:"); 
    String output = ""; 
    for(int i = 0; i < b.length; i++) 
    { 
     output = output + b[i]; 
    } 
    return output; 
}//end toString method 
}//end Translate Class 

주요 방법 :

import javax.swing.JOptionPane; 
import java.io.*; 
import java.util.Scanner; 
public class MorseCodeTester 
{ 
    public static void main(String[] args)throws IOException 
    { 
     String userInput; 
     final String SENTINEL = "0";//for exiting program when entered 
     //object creation 
     MorseCode text = new MorseCode(); 
     //getting user input to be translated 
     do 
     { 
      Scanner in = new Scanner(System.in); 
      System.out.print("Please enter what you wish to translte to Morse code (no punctuation): "); 
      userInput = in.nextLine(); 
      String compare = userInput.toUpperCase(); 
      String[] codedText = new String[compare.length()]; 
      codedText = text.translateHere(compare); 
      text.toString(userInput, codedText); 
     }while(!userInput.equals(SENTINEL)); 
    }//end main 
}//end class 

그래서 내 프로그램은 내가 입력에 넣을 때 출력이 주요 방법에 내가 모스 코드로 변환 할 계산을했다하더라도 그래도 검은 색 표시입니다. 또한 어떤 방법 으로든 정적 식별자를 사용할 수 있는지에 대한 제안이 있습니다. 여기

텍스트 파일입니다

1 .---- 

2 ..--- 

3 ...-- 

4 ....- 

5 ..... 

6 -.... 

7 --... 

8 ---.. 

9 ----. 

0 ----- 

A .- 

B -... 

C -.-. 

D -.. 

E . 

F ..-. 

G --. 

H .... 

I .. 

J .--- 

K -.- 

L .-.. 

M -- 

N -. 

O --- 

P .--. 

Q --.- 

R .-. 

S ... 

T - 

U ..- 

V ...- 

W .-- 

X -..- 

Y -.-- 

Z --.. 
+0

디버거를 사용하고 줄 단위로 코드를 단계별로 실행하십시오. –

+0

나는 아직도 작동하지 않으려 고 노력했다. – user2059140

+0

if (userLetters [j] == morseLetter [k]) {newMessage [j] = morseCode [k];} - 실제로 번역이 전혀 일어나지 않는 것을 보시려면 여기에 인쇄 하시겠습니까? –

답변

2

에 코드를 문자로 모스 부호를 키에 해시 맵을 사용한다,이 코드가 훨씬 간단합니다. 문자에 대한 morsecode를 얻는 것은 간단한 검색입니다.

또한 MorseCode의 생성자에서 데이터를 한 번로드하면됩니다.

하지만 궁금한 점이 있으면 출력물을 인쇄하지 않고있는 것입니다. 그냥 반환하고 콘솔에 표시하지 않습니다.

toString 메서드를 이와 같이 변경하면 무언가를 출력해야합니다.

public String toString(String a, String[] b) { 
    System.out.println("Input: " + a); 
    System.out.print("Output:"); // changed to a print, to avoid newline 
    String output = ""; 
    for (int i = 0; i < b.length; i++) { 
     output = output + b[i]; 
    } 
    System.out.println(output); // this was missing 
    return output; 
}//end toString method 
+0

완벽하게 작동 해 주셔서 감사합니다! – user2059140