2017-01-07 1 views
-1

텍스트 파일에서 명령을 읽는 드로잉 프로그램을 만들고 있는데, 파일을 선택하면 올바른 입력 매개 변수에 대한 명령의 유효성을 검사해야합니다. 프로그램에서 연 텍스트 파일에는 다음이 포함됩니다. 스윙/AWT 파일 선택기 유효성 검사 그래픽 드로잉

Move 100 100 // (move pen to X Y) 
MIVE 100 50 // (Invalid comamnd spelt incorrectly) 
move x y // (invalid command not an integer) 
Line 20 100 // (draw a line at X Y) 

나는 프로그램으로 텍스트 파일을 열 때, 그것은 텍스트 파일을 가져 오는 것이 데 있지만,이 JTextArea로 검증되지 않고 선택된 X/Y의 공동의 선을 그리는 문제 종좌표. 아무도 올바른 방향으로 나를 가리킬 수 있을까요?

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

public class Instructionpanel extends JPanel { 

JTextArea instructions; 

// Move line text clear 
public Instructionpanel(GraphicsPanel graphicspanel) { 

    instructions = new JTextArea(
      "This is where the instructions will displayed", 10, 50); // Rows 
                     // * 
                     // columns 
    instructions.setLineWrap(true); 
    instructions.setWrapStyleWord(true); 
    instructions.setEditable(true); 
    JScrollPane areaScrollPane = new JScrollPane(instructions); 
    areaScrollPane 
      .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    add(areaScrollPane); 
    // add(instructions); 
} 

public void processFile(File file) { 
    Scanner scan = null; 
    try { 
     scan = new Scanner(file); 

    } catch (FileNotFoundException el) { 
     el.printStackTrace(); 
    } 

    String allInstructions = ""; 

    String allInstructions1 = ""; 
    String instruction = ""; 
    while (scan.hasNext()) { 
     instruction = scan.nextLine() + "\n"; 
     // check or validate the instruction 

     allInstructions1 += validateInstruction(instruction); 

    } 

    instructions.setText(allInstructions1); 

} 

public String validateInstruction(String orig) { 
    String returnString = orig; 

    // Do all the checking 
    // Convert string to an array 
    String command = ""; 
    String[] instructionArray = orig.split(" "); 

    int x, y = 0; 

    switch(instructionArray[0]) 
     { 
    case "MOVE": 
     // Check there three parameters 
     doMove(instructionArray);   { 
      // And they are integers 
      instructions = new JTextArea(" Incorrect parameter type i.e 100"); 
      instructions = new JTextArea(":incorrect number of parameters i.e Line 100 200"); 
      try { 

       GraphicsPanel.setPos (Integer.parseInt(instructionArray[1]),Integer.parseInt(instructionArray[2])); 

      } 

      catch (NumberFormatException e) 
      { 
       instructions = new JTextArea(" only numbers are allowed "); 
      } 
     } 
     break; 

      case "LINE": 
       doLine (instructionArray); 
     // Check there three parameters 
     if (instructionArray.length != 3) { 
      // And they are integers 
      instructions = new JTextArea(" Incorrect parameter type i.e 100"); 
      instructions = new JTextArea(":incorrect number of parameters i.e Line 100 200"); 
      try { 
       GraphicsPanel.drawLine (Integer.parseInt(instructionArray[1]),Integer.parseInt(instructionArray[2])); 
      } catch (NumberFormatException e) { 
       instructions = new JTextArea(" only numbers are allowed "); 
      }break; 

      }} 
    return orig;} 

private void doLine(String[] instructionArray) { 
    // TODO Auto-generated method stub 

} 
private void doMove(String[] instructionArray) { 
    // TODO Auto-generated method stub 
} 
} 
+0

더 나은 도움을 받으려면 [mcve]를 게시하십시오. 그것을 MCVE로 만들기 위해서는 Complete (하나의 복사/붙여 넣기)가 필요하므로 소스 코드에서 데이터를 하드 코딩하여'String'으로 저장하고 화면에 표시하는 주요 방법을 포함시켜야합니다. BTW - 귀하가 요청한 첫 번째 질문을 제외하고는 답변을받지 못했습니다. 또한 SSCCE (MCVE와 동등)를 게시하기 위해 적어도 한 번은 사전에 조언을 받았으므로 그렇게하는 이점을 파악하지 못했습니다. SO에 대한 성공적인 솔루션을 얻으려면 이러한 요점을 생각해 볼 수 있습니다. –

+0

그래, 그렇지만 나는 인정했다. 자바에 관해서 가장 친숙한 사용자는 아니지만, 처음부터 시작하는 것은 내 과제가 하루 만에 할 수있는 옵션이 아니다. – Adam

+1

* "내 임무는 하루 만에해야합니다."* 그러면 당신은 더 빨리 일을 시작하고/또는 더 나은 시간 관리를 사용해야합니다. 그리고 앞으로도 우리의 문제를 오해하지 마십시오. 무료로 도움을주는 사람들은 일반적으로 조언을 따르기에 충분한 시간 관리 기술을 가진 사람들을 돕는 것을 선호합니다. –

답변

0

얼핏에서 나는 당신의 토큰이 하나의 공간을두고 있기 때문에, 따라서 instructionArray[0] 전체 라인을 포함, 아마도 orig.split(" ") 이후 단 하나의 요소로 생성하여 String[] instructionArray가 구분 기호로 이중 공간이 있는지 말할 수있다. 따라서 validateInstruction(String orig)에있는 귀하의 사례는 일치하지 않습니다.

이제는 orig.split(" ") 통화에서 이중 공간을 단일 공간으로 대체하는 방법으로 문제를 해결할 수 있습니다.

이렇게 말하면 나는 변화가 일어날 수있는 모든 잠재적 인 문제를 해결할 수 있다고 보장합니다.