Skip to main content
Formatter for integer uses d, not s.
Source Link
MrLore
  • 1.4k
  • 9
  • 16

I'd change your enum State, like this:

enum State
{
    OK(null),
    COULDNT_LOAD("Error: Could not load file: %s."),
    COULDNT_PARSE("Error: Could not parse contents of file %s."),
    UNEXPECTED_NUM_FILES("Error: Unexpected number of files - expected %s%d, actual %s%d.");

    String message;

    State(String message)
    {
        this.message = message;
    }
    
    void setReplacements(Object... replacements)
    {
        if(this.message != null)
        {
            this.message = new Formatter().format(this.message, replacements).toString();
        }
    }
}

Then you can have your validation method return a state, and you can do your checking block after, like this:

File fileITriedToLoad = new File("/usr/file.java");
State result = State.COULDNT_LOAD;
result.setReplacements(fileITriedToLoad.getAbsolutePath());

if(result != State.OK)
{
    System.out.println(result.message);
}

And you can delete the rest of your Result class.

Edit: Using the new setReplacements() method would then allow you to add pieces of text at the specified points in your message, so your validation method that returns a State would use it as follows:

State state = State.UNEXPECTED_NUM_FILES;
state.setReplacements(1, 3);

And then printing the state.message will produce:

Error: Unexpected number of files - expected 1, actual 3.

I'd change your enum State, like this:

enum State
{
    OK(null),
    COULDNT_LOAD("Error: Could not load file: %s."),
    COULDNT_PARSE("Error: Could not parse contents of file %s."),
    UNEXPECTED_NUM_FILES("Error: Unexpected number of files - expected %s, actual %s.");

    String message;

    State(String message)
    {
        this.message = message;
    }
    
    void setReplacements(Object... replacements)
    {
        if(this.message != null)
        {
            this.message = new Formatter().format(this.message, replacements).toString();
        }
    }
}

Then you can have your validation method return a state, and you can do your checking block after, like this:

File fileITriedToLoad = new File("/usr/file.java");
State result = State.COULDNT_LOAD;
result.setReplacements(fileITriedToLoad.getAbsolutePath());

if(result != State.OK)
{
    System.out.println(result.message);
}

And you can delete the rest of your Result class.

Edit: Using the new setReplacements() method would then allow you to add pieces of text at the specified points in your message, so your validation method that returns a State would use it as follows:

State state = State.UNEXPECTED_NUM_FILES;
state.setReplacements(1, 3);

And then printing the state.message will produce:

Error: Unexpected number of files - expected 1, actual 3.

I'd change your enum State, like this:

enum State
{
    OK(null),
    COULDNT_LOAD("Error: Could not load file: %s."),
    COULDNT_PARSE("Error: Could not parse contents of file %s."),
    UNEXPECTED_NUM_FILES("Error: Unexpected number of files - expected %d, actual %d.");

    String message;

    State(String message)
    {
        this.message = message;
    }
    
    void setReplacements(Object... replacements)
    {
        if(this.message != null)
        {
            this.message = new Formatter().format(this.message, replacements).toString();
        }
    }
}

Then you can have your validation method return a state, and you can do your checking block after, like this:

File fileITriedToLoad = new File("/usr/file.java");
State result = State.COULDNT_LOAD;
result.setReplacements(fileITriedToLoad.getAbsolutePath());

if(result != State.OK)
{
    System.out.println(result.message);
}

And you can delete the rest of your Result class.

Edit: Using the new setReplacements() method would then allow you to add pieces of text at the specified points in your message, so your validation method that returns a State would use it as follows:

State state = State.UNEXPECTED_NUM_FILES;
state.setReplacements(1, 3);

And then printing the state.message will produce:

Error: Unexpected number of files - expected 1, actual 3.

deleted 7 characters in body
Source Link
MrLore
  • 1.4k
  • 9
  • 16

I'd change your enum State, like this:

enum State
{
    OK(null),
    COULDNT_LOAD("Error: Could not load file: %s."),
    COULDNT_PARSE("Error: Could not parse contents of file %s."),
    UNEXPECTED_NUM_FILES("Error: Unexpected number of files - expected %s, actual %s.");

    String message;

    State(String message)
    {
        this.message = message;
    }
    
    public void setReplacements(Object... replacements)
    {
        if(this.message != null)
        {
            this.message = new Formatter().format(this.message, replacements).toString();
        }
    }
}

Then you can have your validation method return a state, and you can do your checking block after, like this:

File fileITriedToLoad = new File("/usr/file.java");
State result = State.COULDNT_LOAD;
result.setReplacements(fileITriedToLoad.getAbsolutePath());

if(result != State.OK)
{
    System.out.println(result.message);
}

And you can delete the rest of your Result class.

Edit: Using the new setReplacements() method would then allow you to add pieces of text at the specified points in your message, so your validation method that returns a State would use it as follows:

State sstate = State.UNEXPECTED_NUM_FILES;
sstate.setReplacements("test.java"1, "production.java"3);

And then printing the state.message will produce:

Error: Unexpected number of files - expected test.java1, actual production.java3.

I'd change your enum State, like this:

enum State
{
    OK(null),
    COULDNT_LOAD("Error: Could not load file: %s."),
    COULDNT_PARSE("Error: Could not parse contents of file %s."),
    UNEXPECTED_NUM_FILES("Error: Unexpected number of files - expected %s, actual %s.");

    String message;

    State(String message)
    {
        this.message = message;
    }
    
    public void setReplacements(Object... replacements)
    {
        if(this.message != null)
        {
            this.message = new Formatter().format(this.message, replacements).toString();
        }
    }
}

Then you can have your validation method return a state, and you can do your checking block after, like this:

File fileITriedToLoad = new File("/usr/file.java");
State result = State.COULDNT_LOAD;
result.setReplacements(fileITriedToLoad.getAbsolutePath());

if(result != State.OK)
{
    System.out.println(result.message);
}

And you can delete the rest of your Result class.

Edit: Using the new setReplacements() method would then allow you to add pieces of text at the specified points in your message, so your validation method that returns a State would use it as follows:

State s = State.UNEXPECTED_NUM_FILES;
s.setReplacements("test.java", "production.java");

And then printing the state.message will produce:

Error: Unexpected number of files - expected test.java, actual production.java.

I'd change your enum State, like this:

enum State
{
    OK(null),
    COULDNT_LOAD("Error: Could not load file: %s."),
    COULDNT_PARSE("Error: Could not parse contents of file %s."),
    UNEXPECTED_NUM_FILES("Error: Unexpected number of files - expected %s, actual %s.");

    String message;

    State(String message)
    {
        this.message = message;
    }
    
    void setReplacements(Object... replacements)
    {
        if(this.message != null)
        {
            this.message = new Formatter().format(this.message, replacements).toString();
        }
    }
}

Then you can have your validation method return a state, and you can do your checking block after, like this:

File fileITriedToLoad = new File("/usr/file.java");
State result = State.COULDNT_LOAD;
result.setReplacements(fileITriedToLoad.getAbsolutePath());

if(result != State.OK)
{
    System.out.println(result.message);
}

And you can delete the rest of your Result class.

Edit: Using the new setReplacements() method would then allow you to add pieces of text at the specified points in your message, so your validation method that returns a State would use it as follows:

State state = State.UNEXPECTED_NUM_FILES;
state.setReplacements(1, 3);

And then printing the state.message will produce:

Error: Unexpected number of files - expected 1, actual 3.

added 66 characters in body
Source Link
MrLore
  • 1.4k
  • 9
  • 16

I'd change your enum State, like this:

enum State
{
    OK(null),
    COULDNT_LOAD("Error: Could not load file: %s."),
    COULDNT_PARSE("Error: Could not parse contents of file %s."),
    UNEXPECTED_NUM_FILES("Error: Unexpected number of files - expected %s, actual %s.");

    String message;

    State(String message)
    {
        this.message = message;
    }
    
    public void setReplacements(Object... replacements)
    {
        if(this.message != null)
        {
            this.message = new Formatter().format(this.message, replacements).toString();
        }
    }
}

Then you can have your validation method return a state, and you can do your checking block after, like this:

File fileITriedToLoad = new File("/usr/file.java");
State result = State.COULDNT_LOAD;
result.setReplacements(fileITriedToLoad.getAbsolutePath());

if(result != State.OK)
{
    System.out.println(result.message);
}

And you can delete the rest of your Result class.

Edit: Using the new setReplacements() method would then allow you to add pieces of text at the specified points in your message, so your validation method that returns a State would use it as follows:

State s = State.UNEXPECTED_NUM_FILES;
s.setReplacements("test.java", "production.java");

And then printing the state.message will produce:

Error: Unexpected number of files - expected test.java, actual production.java.

I'd change your enum State, like this:

enum State
{
    OK(null),
    COULDNT_LOAD("Error: Could not load file: %s."),
    COULDNT_PARSE("Error: Could not parse contents of file %s."),
    UNEXPECTED_NUM_FILES("Error: Unexpected number of files - expected %s, actual %s.");

    String message;

    State(String message)
    {
        this.message = message;
    }
    
    public void setReplacements(Object... replacements)
    {
        if(this.message != null)
        {
            this.message = new Formatter().format(this.message, replacements).toString();
        }
    }
}

Then you can have your validation method return a state, and you can do your checking block after, like this:

File fileITriedToLoad = new File("/usr/file.java");
State result = State.COULDNT_LOAD;

if(result != State.OK)
{
    System.out.println(result.message);
}

And you can delete the rest of your Result class.

Edit: Using the new setReplacements() method would then allow you to add pieces of text at the specified points in your message, so your validation method that returns a State would use it as follows:

State s = State.UNEXPECTED_NUM_FILES;
s.setReplacements("test.java", "production.java");

And then printing the state.message will produce:

Error: Unexpected number of files - expected test.java, actual production.java.

I'd change your enum State, like this:

enum State
{
    OK(null),
    COULDNT_LOAD("Error: Could not load file: %s."),
    COULDNT_PARSE("Error: Could not parse contents of file %s."),
    UNEXPECTED_NUM_FILES("Error: Unexpected number of files - expected %s, actual %s.");

    String message;

    State(String message)
    {
        this.message = message;
    }
    
    public void setReplacements(Object... replacements)
    {
        if(this.message != null)
        {
            this.message = new Formatter().format(this.message, replacements).toString();
        }
    }
}

Then you can have your validation method return a state, and you can do your checking block after, like this:

File fileITriedToLoad = new File("/usr/file.java");
State result = State.COULDNT_LOAD;
result.setReplacements(fileITriedToLoad.getAbsolutePath());

if(result != State.OK)
{
    System.out.println(result.message);
}

And you can delete the rest of your Result class.

Edit: Using the new setReplacements() method would then allow you to add pieces of text at the specified points in your message, so your validation method that returns a State would use it as follows:

State s = State.UNEXPECTED_NUM_FILES;
s.setReplacements("test.java", "production.java");

And then printing the state.message will produce:

Error: Unexpected number of files - expected test.java, actual production.java.

Changed to fit new requirements.
Source Link
MrLore
  • 1.4k
  • 9
  • 16
Loading
Made message final, best practice
Source Link
MrLore
  • 1.4k
  • 9
  • 16
Loading
Java naming conventions for constants is uppercase and underscore separated words.
Source Link
MrLore
  • 1.4k
  • 9
  • 16
Loading
Shortened
Source Link
MrLore
  • 1.4k
  • 9
  • 16
Loading
Source Link
MrLore
  • 1.4k
  • 9
  • 16
Loading