Swing / AWT filechooser validation graphics drawing

117 Views Asked by At

I'm making a drawing program which reads commands from a text file, when file is selected the program should validate the commands for correct entry parameters. the text file opened in the program includes;

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)

The problem that I am having is that upon opening the text file into the program, it is importing the text file but it is not validating it into the JTextArea and drawing the line at selected X / Y co-ordinates. Could anybody point me in the right direction?

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  
}
}
1

There are 1 best solutions below

0
On

From a quick look I can say that your String[] instructionArray probably created with only one element since the orig.split(" ") has double space as a delimiter, therefore instructionArray[0] contains the whole line since your tokens are spaced with only one space. Therefore none of your cases in validateInstruction(String orig) are matched.

For now you can probably get away with replacing the double space to single space in the orig.split(" ") call.

That being said I cant guaranty this will solve all the potential issues that might occur after the change.