How do I exit my program without using return and System.exit()?

3k views Asked by At

So this is my code :-

import java.io.*;
class RAILWAYS
{
    BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
    void main () throws IOException
    {
        System.out.print("\f");
        System.out.println("Welcome to IRCTC Railway Reservation System! Please proceed further     to book your train!");
    System.out.println();
    String[] trainname = {"Rajdhani EXP", "AUG Kranti EXP", "Chennai EXP", "Aravali EXP", "Paschim EXP", "Gareeb Rath", "Punjab Mail", "Dehradun EXP", "Swaraj EXP", "Aravali EXP"};
    int[] trainno = {57835, 87612, 15384, 16512, 65265, 51654, 31543, 56416, 85484, 78455};
    String[] origin = {"Mumbai", "Mumbai", "Delhi", "Kolkata", "Mumbai", "Goa", "Durg", "Aligarh", "Jaipur", "Bhuj"};
    String[] destination = {"Surat", "Delhi", "Chennai", "Mumbai", "Ajmer", "Mumbai", "Bikaner", "Agra", "Madurai", "Buxar"};
    int[] fare = {650, 950, 1100, 1200, 1050, 600, 1100, 1250, 1300, 1100};
    int[] lengths = {trainname.length, trainno.length, origin.length, destination.length, fare.length};
    String[] sd = new String[3];
    int in[] = new int[3];
    System.out.println("Train Name\t\tTrain No.\tOrigin\t        Destination\tFare");
    System.out.println();
    for (int i=0;i<lengths[0];i++)
    {
        System.out.println(trainname[i]+"\t\t"+trainno[i]+"\t\t"+origin[i]+"\t\t"+destination[i]+"\t\t"+fare[i]); 
    }
    System.out.println();
    System.out.print("Enter train no. to select train or enter 1 to exit :- ");
    in[0] = Integer.parseInt (br.readLine());
    if (in[0]==1)
    {
        System.out.print("Thanks for visiting our website!");
        return;
    }
    else
    {
         for (int j=0;j<lengths[0];j++)
         {
             if (trainno[j]==in[0])
             {
                 sd[0] = trainname[j];
                 sd[1] = origin[j];
                 sd[2] = destination[j];
                 in[1] = fare[j];
             }
         }
    }
    if (in[1]==0)
    {
        System.out.print("Wrong input! Try again....");
        return;
    }
    System.out.print("Enter number of passengers (max 5) :- ");
    in[2] = Integer.parseInt (br.readLine()); 
    if (in[2]>5)
    {
        System.out.print("Uh-Oh! No. of passengers are more than 5, please try again");
        return;
    }
    String[] pn = new String[in[2]];
    for (int k=0;k<in[2];k++)
    {
        System.out.print("Enter passenger's name :- ");
        pn[k] = br.readLine();
    }
    System.out.println();
    System.out.println("Booking Details :-");
    System.out.println();
    System.out.println("No. of passengers :- "+in[2]);
    for (int z=0;z<in[2];z++)
    {
        System.out.println("Name of passenger travelling :- "+pn[z]);
    }
    System.out.println("Train Name :- "+sd[0]);
    System.out.println("Train Number :- "+in[0]);
    System.out.println("Train Origin :- "+sd[1]);
    System.out.println("Train Destination :- "+sd[2]);
    System.out.println("Train Fare/Person :- "+in[1]);
    System.out.println("Total Fare :- "+in[2]*in[1]);
    System.out.println();
    System.out.println("Thanks for your booking! Your seats have been confirmed. Have a good day!");
}
}

As you can see I have used return many times here but my teacher is one big pain and says that i can't use return or System.exit(0) :P I really need to terminate the program in my if statements as i cat use return or System.exit(0) :P I have heard that this is possible with while or do-while loop but i can't understand how...

Help will always be appreciated :P

3

There are 3 answers

3
Stephen C On

You could throw an exception when you get bad input, and either catch it, or allow it to kill the program.

However I don't think that is what your teacher is trying to tell you. I think he/she is actually trying to get you to make the program ask (repeatedly) for a valid input if the user enters something invalid.

And in fact, that is what the messages you are outputting in the event of an error imply you should be doing.

(It is possible to restructure your code so that the main method has a single point of return; i.e. at the end. There are a variety of ways, you could do that. But, if your teacher is asking you to do that, they are not being helpful, IMO.)

1
anand ojha On

You have used all 'if' conditions to return error messages and you are processing positive scenarios if the system doesn't satisfy those 'if' conditions. You could replace that with giving an 'if' condition for valid scenarios and using 'else' condition after that to return error messages. This way you won't have to explicitly return.

e.g : The last part of ur program could be written like :

if (in[2]<=5)
    {

    String[] pn = new String[in[2]];
    for (int k=0;k<in[2];k++)
    {
        System.out.print("Enter passenger's name :- ");
        pn[k] = br.readLine();
    }
    System.out.println();
    System.out.println("Booking Details :-");
    System.out.println();
    System.out.println("No. of passengers :- "+in[2]);
    for (int z=0;z<in[2];z++)
    {
        System.out.println("Name of passenger travelling :- "+pn[z]);
    }
    System.out.println("Train Name :- "+sd[0]);
    System.out.println("Train Number :- "+in[0]);
    System.out.println("Train Origin :- "+sd[1]);
    System.out.println("Train Destination :- "+sd[2]);
    System.out.println("Train Fare/Person :- "+in[1]);
    System.out.println("Total Fare :- "+in[2]*in[1]);
    System.out.println();
    System.out.println("Thanks for your booking! Your seats have been confirmed. Have a good day!");
} else{
System.out.print("Uh-Oh! No. of passengers are more than 5, please try again");

    }
5
nirsky On

You can wrap it all with

while(true){ //YOUR CODE HERE}

and then replace return; with

break;

to stop the loop or

continue;

to go to the next iteration and try again.