Hack Forums
[Java] Switch statement validation JTextArea / JFilechooser? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Switch statement validation JTextArea / JFilechooser? (/showthread.php?tid=5512905)



Switch statement validation JTextArea / JFilechooser? - Advmx - Jan 3, 2017

I was wondering if anybody could help me validating a text file from a JFileChooser I want it to select only certain 'cases' i've attempted to do this with the switch statement but i'm lost, then placing the validated text into the Jtextarea at the bottom, then from there draw it in on the canvas (dark grey area). I've tried repeatedly and I'm truly lost if anybody could adjust my code accordingly or point me in the right direct. Will pay if required just pm me your skype name (y)

InstructionsPanel.java
pastebin.com/ufz6cdQ3

DrawingAssignment.java
pastebin.com/r84vFnVT

GraphicsPanel.java
pastebin.com/SrddUBHp

Load text file.txt
Code
LINE 100 100 // Corresponds to draw LINE at X and Y
MOVE 30 40 // Corresponds to move pen to X and Y
COLOR 255 100 10 // corrosponds to setting the color of pen to correct RGB colors



* - Sogomn - Jan 4, 2017

Do you want to validate the contents of the text file or the file name?


* - Advmx - Jan 5, 2017

(Jan 4, 2017 08:45 AM)Sogomn Wrote:  Do you want to validate the contents of the text file or the file name?

The contents of the file.


* - Sogomn - Jan 6, 2017

Well then just read the file, iterate through all its lines and check if they are valid:

Code
final Path file = fileChooserFile.toPath();
final List<String> lines = Files.readAllLines(file);

for (final String line : lines) {

    /*
    * You might want to use only the first word of the line or whatever
    * Just use calls like "line.substring(0, end)" then, for example
    */

    final boolean valid = validate(line);

    if (valid) {
        //Do your magic
    }
}

The "validate" method could look like this:

Code
boolean validate(final String line) {
    switch (line) {
    case "Bla":
    case "Whatever":
    case "Valid stuff":
        return true;
    default:
        return false;
    }
}

A better way would be to have a list which contains all valid items. Then you just need to check if the string contains the item.