1

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

8
  • You can try user defined exception in place of return. Commented Nov 11, 2014 at 11:03
  • @tobias_k i need to use something else for the return statements i have used in my if statements in the program.. i know that the program terminates after completing the last statement -_- Commented Nov 11, 2014 at 11:04
  • @starlord he hasn't taught us that too :P :'( Commented Nov 11, 2014 at 11:06
  • Has he said WHY you can't use return ? Commented Nov 11, 2014 at 11:08
  • @Stewart because he hasn't taught us that -_- as i mentioned earlier he is a real pain in the a** Commented Nov 11, 2014 at 11:10

3 Answers 3

2

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");

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Trying that now, will post if i finally get it. Thank btw :D
1

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.

5 Comments

Can u just give me an example of continue to try again?
continue; will just go to the next iteration (to the beginning of the while).
Not that bro for example in my second if :- if (in[1]==0) { System.out.print("Wrong input! Try again...."); return; } if here i put continue instead of break, it won't print the table back again
Simply place your printings before starting the while loop.
if you would please tick the check-mark next to my answer if it answered your question.
0

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.)

3 Comments

I can do that too but i can't figure out how to do that too :P total noob here :P i know we can do it with while and do while, i did try it but it never worked and the program never worked as it should have
I suggest you ask your teacher what he or she really wants you to do.
I'm pretty sure that a**hole wont tell me, this is what happens in India -_-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.