1

I know i need this code looped but I'm not sure how to do it, like i don't know what to have as the loop? it clears all the textfields at the minute, but i only want the textfield that has anything but an integer cleared. any help would be appreciated.

try {
    int a = Integer.parseInt(theApp.tred.getText());
    int b = Integer.parseInt(theApp.tgreen.getText()); // uses
                                                        // information
                                                        // entered
    int c = Integer.parseInt(theApp.tblue.getText());

    if (a < 0) {
        a = 200; // if statements for values above and below the targets
                            // set
        tred.setText("200");
    }

    if (a > 255) {
        a = 255;
        tred.setText("255");
    }
    if (b < 0) {
        b = 200;
        tgreen.setText("200");
    }

    if (b > 255) {
        b = 255;
        tgreen.setText("255");
    }

    if (c < 0) {
        c = 200;
        tblue.setText("200");
    }
    if (c > 255) {
        c = 255;
        tblue.setText("255");
    }
    message.setText(" work submitted by:"); // text
    message.setForeground(new Color(a, b, c)); // changes colour to
                                                        // desired input

} catch (NumberFormatException ex) {

    message.setText("invalid input! please enter numbers only"); // text
    message.setForeground(new Color(0, 0, 0)); // original text set to
                                                // red
    tred.setText("");
    tgreen.setText("");
    tblue.setText(""); // clears box if not an integer
}
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
sb33
  • 67
  • 7
  • What are you actually trying to achieve? – aleb2000 Nov 05 '16 at 19:30
  • @aleb2000 i have more code, but basically you enter 3 values (red green and blue) and it changes a message to the colour that represents the values entered. I'm just trying to do some extra work that deals with random entries like numbers above 255 or below 0 or anything that isn't an int – sb33 Nov 05 '16 at 19:32
  • Here I don't think you need a loop, instead you could try adding a listener to the textfield document implementing the `changeUpdate()` method like in this answer: http://stackoverflow.com/a/3953219/5955649 – aleb2000 Nov 05 '16 at 19:36
  • Sorry I was partially wrong, in the documentation is said that plain text components do not fire the `chagedUpdate()` method so you have to override the `insertUpdate()` and `removeUpdate()` methods of the same interface. – aleb2000 Nov 05 '16 at 19:46
  • did you manage to solve it? – ItamarG3 Nov 05 '16 at 19:59
  • @aleb2000 lol it all sounds a bit complicated, i haven't learnt that yet, i got told it would be useful to use a loop, any other suggestions? – sb33 Nov 05 '16 at 20:00
  • @ItamarGreen nah not yet – sb33 Nov 05 '16 at 20:00
  • @sb33 have you tried my answer? – ItamarG3 Nov 05 '16 at 20:08

1 Answers1

1

You can separate the try-catch block into 3 parts:

int a = -1;
try {
    a = Integer.parseInt(theApp.tred.getText());
    if (a < 0) {
        a = 200; 
        tred.setText("200");
    }
    if (a > 255) {
        a = 255;
        tred.setText("255");
    }
    //do the needed things here
} catch (Exception e) {
    message.setText("invalid input! please enter numbers only"); // text
    message.setForeground(new Color(0, 0, 0)); 
    tred.setText("");
}

(this is just for tred, the others are pretty much the same).

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • 1
    just tried that but if i do that the message.setforeground that changes the message to the desired input can't be done, because int a b c are defined in their own try's and can't be calle outside of them? – sb33 Nov 05 '16 at 20:11
  • @sb33 have you implemented the change? – ItamarG3 Nov 05 '16 at 20:14
  • it clears the field now! which is good, it just displays the wrong message but ill try and sort that – sb33 Nov 05 '16 at 20:18
  • @sb33 if the answer is good, then you should upvote it and marks it as accepted. that way, others know this answer works – ItamarG3 Nov 05 '16 at 20:19