-3

im just wondering is there any goto or continue commands for if statement ?? im using continue; but its not working on if statement.

                       if ( typess == 1){
                ***OUTER:***

                     System.out.println("Type: Current Minimum of 1000.00 ");
                    System.out.print("Amount: " );
                    currenttype = Float.parseFloat(in.readLine());
                    if ( currenttype >= 1000 ){
                    balance += currenttype;
                    System.out.print("Successful " + currenttype + " Added to your Account");

                    }

                    else
                    {
                        **continue OUTER;**
                      System.out.print("MINIMUM IS 1000.00 ");

               }
  • what? can you please make the code clearer (format it and clarify what the question is) – ItamarG3 Oct 28 '16 at 07:51
  • 3
    What do you want to happen? Do you want the if clause to be executed even if the condition is false? That doesn't make much sense. It looks like you may need a loop. – Eran Oct 28 '16 at 07:52
  • 1
    What you describe as `go-to` can/ has to be solved with methods. You recursivly call it whenever the input is invalid. – SomeJavaGuy Oct 28 '16 at 07:52
  • 1
    You can specify a labelled line as the target of a `break` or `continue` statement, provided you're using a construct where those statements are valid, but there's almost always a better way of doing it. – jsheeran Oct 28 '16 at 07:53

2 Answers2

2

You could solve it with a simple recursive method.

Just include the logical part into a method, validate the input and if it´s incorrect, make an recursiv call, like in this example:

public class Project {

    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        float float_val = getInput();
        System.out.println("You did input: " + float_val);
    }

    public static float getInput() {
        System.out.println("Please input variable");
        float input = scanner.nextFloat();
        if(input < 0) {
            System.out.println("Invalid input!");
            return getInput();
        }
        return input;
    }
}

sample input:

-5 
-4
5

sample output:

Please input variable
-5
Invalid input!
Please input variable
-4
Invalid input!
Please input variable
5
You did input: 5.0
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
1

There is such posibility, but you should use do-while loop instead, e.g.

boolean printMsg = false;
do {
    if (printMsg) {
        System.out.print("MINIMUM IS 1000.00 ");
    }
    printMsg = true;
    System.out.println("Type: Current Minimum of 1000.00 ");
    System.out.print("Amount: " );
    currenttype = Float.parseFloat(in.readLine());
} while (currenttype < 1000);

balance += currenttype;
System.out.print("Successful " + currenttype + " Added to your Account");
Jhon D.
  • 71
  • 2