Rasmus Rikken Rasmus Rikken - 12 days ago 6
Java Question

How can I return to the beginning of my program if something goes wrong?

Shortened question: How can I return to the beginning of my code in case something goes wrong?

A little bit more detailed: At the end of my code I have an int variable named "tulemus". I'd like it to tell me that "Tulemus is wrong, please try again!" if

(tulemus < 0 && tulemus > 110)
is true, and also return to the line where the user types in the number (
System.out.println("Sisesta 1. iseseisva töö tulemused(0-20 punkti): "); String str = input.readLine();
), to allow re-doing this operation without having to restart the program.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Yl1osaB {

public static void main(String[] args) throws IOException {

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Ylesanne 1 RASMUS RIKKEN 25/11/2016");

System.out.println("Sisesta oma nimi:");
String nimi = input.readLine();
System.out.println("Sinu nimi:" + nimi);

System.out.println("Sisesta 1. iseseisva töö tulemused(0-20 punkti): ");
String str = input.readLine();
int number1 = Integer.parseInt(str);

if (number1 > 20) {
System.out.println("Sisestatud number on suurem kui max tulemus.");

} else if (number1 < 0) {
System.out.println("Sisestatud number on väiksem kui min tulemus.");

}

System.out.println("Sisesta 2. iseseisva töö tulemused(0-20 punkti): ");
String str2 = input.readLine();
int number2 = Integer.parseInt(str2);
System.out.println("Iseseisev 1 ja 2 tulemused:" + (number2 + number1));

if (number2 > 20) {
System.out.println("Sisestatud number on suurem kui maxtulemus.");
return;
} else if (number2 < 0) {
System.out.println("Sisestatud number on väiksem kui min tulemus.");
return;
}
System.out.println("Sisesta 3. iseseisva töö tulemused(0-20 punkti): ");
String str3 = input.readLine();
int number3 = Integer.parseInt(str3);
System.out.println("Iseseisev 1 ja 2 ja 3 tulemused:" + (number2 + number1 + number3));

if (number3 > 20) {
System.out.println("Sisestatud number on suurem kui max tulemus.");
return;
} else if (number3 < 0) {
System.out.println("Sisestatud number on väiksem kui min tulemus.");
return;
}
System.out.println("Sisesta koduste tööde punktid(max 10 punkti): ");
String str4 = input.readLine();
int number4 = Integer.parseInt(str4);
System.out.println("Iseseisev 1 ja 2 ja 3 ja koduste ül. tulemused:" + (number2 + number1 + number3 + number4));

if (number4 > 10) {
System.out.println("Sisestatud number on suurem kui max tulemus.");
return;
} else if (number4 < 0) {
System.out.println("Sisestatud number on väiksem kui min tulemus.");
return;
}

System.out.println("Sisesta eksami punktid(max 40 punkti): ");
String str5 = input.readLine();
int number5 = Integer.parseInt(str5);
System.out.println("Iseseisev 1 ja 2 ja 3 ja koduste ül. ja eksami tulemused:"
+ (number2 + number1 + number3 + number4 + number5));

if (number5 > 40) {
System.out.println("Sisestatud number on suurem kui eksami max tulemus.");
return;
} else if (number5 < 0) {
System.out.println("Sisestatud number on väiksem kui eksami min tulemus.");
return;
}
int tulemus = 0;
tulemus = number1 + number2 + number3 + number4 + number5;

if (tulemus > 0 && tulemus < 50) {
System.out.println("Tulemus on " + tulemus + ", mis on alla 50 punkti, Aine on mittearvestatud.");
} else if (tulemus <= 110) {
System.out.print("Kokku on " + tulemus + " punkti. Aine on arvestatud!");
}
if (tulemus <= 110 && tulemus >= 91) {
System.out.print(" Suurepärane!");
}

else if (tulemus <= 90 && tulemus >= 81) {
System.out.print(" Väga hea!");
}

else if (tulemus <= 80 && tulemus >= 71) {
System.out.print(" Hea!");
}

else if (tulemus <= 70 && tulemus >= 61) {
System.out.print(" Rahuldav!");
}

else if (tulemus <= 60 && tulemus >= 51) {
System.out.print(" Kasin!");
} else if (tulemus <= 50 && tulemus >= 0) {
System.out.print(" Puudulik!");
}

} // MAIN
} // CLASS

Answer

Learn the Java language, then you will find that a while loop or a do-while loop will be what you are after. Just define tulemus before the loop to be able to check it in the condition, or define some boolean outside the loop that you set accordingly inside the loop, or make an endless loop with while(true) { } and then quit the loop with break; if your failure condition is not matched.

Besides that (tulemus < 0 && tulemus > 110) will always be false.

So something like

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Yl1osaB {

    public static void main(String[] args) throws IOException {

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Ylesanne 1 RASMUS RIKKEN 25/11/2016");

        while (true) {

            // your code

            if(tulemus < 0 || tulemus > 110) {
                System.out.println("Tulemus is wrong, please try again!");
            }
            else {
                break;
            }
        }

    } // MAIN
}