2012-08-07 3 views
0

내 프로그램의 모든 오류에 대해 댓글을 달았습니다. 내 프로그램의 요점은 내 입력 파일을 사용자가 무엇을 입력하여 이동하는 것입니다. 내가 주석 처리 한 오류는 모두 의미가 없기 때문에 무의미합니다. 컴퓨터가 원하는 방식으로 컴퓨터를 읽지 못하기 때문입니다. 내가 토큰 중 하나에 대해 언급 한 오류 중 하나와 혼동해서는 안됩니다. "," 다른 것은 "("및 ")"입니다. 이 모든 것들은 내가 주석을 달았던 라인의 어딘가에서 세미 콜론을 예상하는 오류이다.토큰에 구문 오류,; 기대 되는가?

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

class CaesarCipher 
{ 
public static void main (String [] args) throws FileNotFoundException 
{ 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("What shift should I use? "); 
    int shift = keyboard.nextInt(); 
    System.out.println("What is the name of the input file? "); 
    String name = keyboard.next(); 
    File f = new File(name); 
    Scanner inFile = new Scanner(f); 
    System.out.println("What is the name of the output file? "); 
    String text = keyboard.nextLine(); 
    PrintWriter outFile = new PrintWriter(text); 
    String encrypted = ""; 

    while (inFile.hasNextLine()) 
    { 
    String line = inFile.nextLine(); 
     if (shift == 1) 
    encrypted = caesarEncipher(line, shift); 
     else if (shift == 2) 
    encrypted = caesarDecipher(line, shift);// the method caesarDecipher(java.lang.String, int) is undefined for the type CaesarCipher 
     System.out.println(encrypted); 
     outFile.println(encrypted); 
    } 
} 
    static String caesarEncipher(String text ,int shift) throws FileNotFoundException 
    { 
    String t = ""; 
    int i = 0; 
    while (i < t.length()) 
    { 
    if (shift < 0) 
    { 
     shift = (shift % 26) + 26; 
    } 
     int move = (char) ((text.charAt(0) - 'A' + shift) % 26 + 'A'); 
     t += move; 
     i++; 
     System.out.println(t); 
     outFile.println(t); 
     return "DONE!"; 
    } 
                  // for each token listed, it expects the semi colon. 
    static String caesarDecipher(String text, int shift) throws FileNotFoundException // Syntax error on token "(", "," , ")", ; expected  
    { 
     return caesarEncipher(input, -shift); 
     } 
    } 
    } 
+6

당신은'caesarEncipher' 메쏘드에서'}'을 닫지 않습니다. – pb2q

+2

코드를 올바르게 포맷하면 컴퓨터가 어떻게 작동하는지 (대부분의 경우) 읽을 수 있습니다. –

답변

3

귀하의 caesarDecipher 방법을 정의하는 방법 caesarEncipher에 포함 : 여기

는 같은 프로그램 모습입니다. 그건 합법적 인 Java가 아닙니다. 그리고 당신은 빈약 한 컴파일러를 혼동했습니다.

엄밀한 들여 쓰기는 이러한 종류의 것을 매우 명확하게 만듭니다. 이클립스를 들어

: Ctrl 키 + 시프트 + F 당신은 IDE, 또는 이맥스를 사용하는 경우, (유닉스에서 줄 도구가 또한 명령) 전체 파일을 재 들여 공구를 찾아 이맥스를 들어

: 다음 강조 표시 영역 (전체 파일) : Esc를Ctrl 키 + 또는 Alt 키 \ + Ctrl 키 + \

2

caesarEncipher 메서드에서} 이러한 오류를 더 빨리 찾아내는 의도를 사용하십시오.

+0

감사합니다. outFile을 확인할 수 없다는 말입니다. 내 주요 함수에서 outFile을 정의하여 모든 메서드에서 액세스 할 수 있도록했습니다. 문제가 무엇인지 아십니까? –

+0

Java에서 메소드 내에서 변수 (예 : outFile)를 선언하면 범위가 메소드로 제한됩니다. 다른 방법에서는 이러한 로컬 변수에 액세스 할 수 없습니다. 변수를 매개 변수로 전달하거나 다른 매개 변수로 선언해야합니다 (예 : 정적 메서드에서 사용하지 않으려는 경우 정적 클래스 멤버로 사용합니다. – Pyranja

+0

내 변수 outFile을 어떻게 선언 하시겠습니까? 새로 이동 한 텍스트로 끝까지 인쇄하고 싶습니다. –

0

당신은 당신의

static String caesarEncipher(String text ,int shift) throws FileNotFoundException 

의 말에 }을 놓치고있어 당신은이^방법으로 반환하는 경우에도주의

static String caesarDecipher(String text, int shift) throws FileNotFoundException 

의 끝에 여분의 하나가, 이 메서드에서 정의되지 않은 변수 input을 사용하고 있습니다. 어쩌면 당신은 당신의 주장 인 text입니다.

0

앞에서 말한 것처럼 중괄호가 없습니다. 이러한 실수를 발견하려면 IDE을 사용하는 것이 좋습니다. 많은 사람들이 eclipse을 선호하지만 나는 개인적으로 Intellij을 좋아합니다. Eclipse는 무료이며 intellij의 정식 버전은 그렇지 않습니다.

관련 문제