how do restart program at certain point?

Discussion in 'Mac Programming' started by mcmacmcmac, Oct 10, 2007.

  1. mcmacmcmac macrumors member

    Joined:
    Oct 2, 2007
    #1
    Is there a simple way to have the program restart after the error message below is printed?

    if (choice > 3) // display an error message indicating an invalid selection
    System.out.println("Your selection is invalid. Please try again.");
     
  2. mcmacmcmac thread starter macrumors member

    Joined:
    Oct 2, 2007
    #2
    And instead of " (choice > 3) " is there an easy way to specify any value other than 1,2, or 3.
     
  3. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #3
    to catch an error use
    Code:
    try{
    code
    }
    catch("enter in error" e){
    System.out.println("there is an error");
    }
    
     
  4. mcmacmcmac thread starter macrumors member

    Joined:
    Oct 2, 2007
    #4
    Reading from my textbook now.. would it be possible to use the OR opertaor for this to get something like

    if((choice > 3) II (choice <1))


    would that work? if so, any idea how to make the lines for the OR.. I don't see them on my keybaord.. lol.. i must sound retarded
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Ok, you don't mean restart: the program hasn't stopped. What you appear to be asking is: "is there a way to keep asking for a value until one that passes validation is entered?" The answer is yes.

    I'm not sure how you are getting the choice. I'll leave that as choice = getLine(); for the moment, replace this with the actual code you are using :)

    Code:
    int choice = 0
    while (choice <1 && choice >3)
    {
    System.out.println("Please enter a choice between 1 and 3");
    choice = getLine();
    }
    
    This will keep looping until a valid choice is entered.

    As to where the "line" is (this is the pipe character), on my keyboard (UK laptop) it's on the same key as \ which is beside return.
     
  6. mcmacmcmac thread starter macrumors member

    Joined:
    Oct 2, 2007
    #6

    OK I really have a feeling that does exactly what I want but not sure how to change my code to incorporate it!! I'm very very new to this stuff and really appreciate the help but I completely understand if you don't want to bother explaining this further.


    Code:
    import java.util.Scanner; // program uses class Scanner
    
    
    public class AreaOfShapes
    {
    
    
    	public static void main( String args[] )
    	{
    
    		System.out.println("Student Name: Bob\nStudent ID: 999\n\n");
    
    		// create scanner object to obtain input from user
    		Scanner input = new Scanner(System.in);
    
    		// display choices of shapes for the user to choose from
    		System.out.println("Choose one of the following options:\n1 for calculating the area of a circle");
    		System.out.print("2 for calculating the area of a square\n3 for calculating the area of a rectangle\nEnter your choice:");
    		int choice = input.nextInt(); // read the choice from user
    
    		if (choice > 3) // display an error message indicating an invalid selection
    			System.out.println("Your selection is invalid. Please try again.");
    		else if (choice == 1) // calculate the area of a circle
    			{
    			System.out.print("\nEnter a value for radius of the circle:");
    			double radius = input.nextDouble();
    			double area = (3.14 * (Math.pow(radius,2)));
    			System.out.printf("The area of the circle is %.2f\n", area);
    			}
    		else if (choice == 2) // calculate the area of a square
    			{
    			System.out.print("\nEnter a value for the side of the square:");
    			double side = input.nextDouble();
    			double area = (side * side);
    			System.out.printf("The area of the square is %.2f\n", area);
    			}
    		else if (choice == 3) // calculate the area of a rectangle
    			{
    			System.out.print("\nEnter a value for the length of the rectangle: ");
    			double length = input.nextDouble();
    			System.out.print("Enter a value for the width of the rectangle: ");
    			double width = input.nextDouble();
    			double area = (width * length);
    			System.out.printf("The area of the rectangle is %.2f\n", area);
    			}
    
    	} // end method Main
    
    } // end class AreaOfShapes
     
  7. mcmacmcmac thread starter macrumors member

    Joined:
    Oct 2, 2007
    #7
    Well I'll just be submitting this assignment as is.. thanks to those who've replied, much appreciated :)
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Well "Bob" that was kind of the idea: it was meant to point you in the correct direction so as you could go and do some research and find out the correct answer for yourself. If you want to get anywhere in programming you need to be willing to find stuff out for yourself, read the documentation and experiment. Of course it may be that your teacher/tutor has not introduced loops yet so might be surprised if you used one!
     
  9. MongoTheGeek macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #9
    I think you mean (choice < 1 || choice > 3)
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Erm, yes that looks better!
     

Share This Page