Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mcmacmcmac

macrumors member
Original poster
Oct 2, 2007
86
0
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.");
 

mcmacmcmac

macrumors member
Original poster
Oct 2, 2007
86
0
And instead of " (choice > 3) " is there an easy way to specify any value other than 1,2, or 3.
 

italiano40

macrumors 65816
Oct 7, 2007
1,080
0
NY
to catch an error use
Code:
try{
code
}
catch("enter in error" e){
System.out.println("there is an error");
}
 

mcmacmcmac

macrumors member
Original poster
Oct 2, 2007
86
0
And instead of " (choice > 3) " is there an easy way to specify any value other than 1,2, or 3.

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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

mcmacmcmac

macrumors member
Original poster
Oct 2, 2007
86
0
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.


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
 

mcmacmcmac

macrumors member
Original poster
Oct 2, 2007
86
0
Well I'll just be submitting this assignment as is.. thanks to those who've replied, much appreciated :)
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.

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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.