This is my assignment, before some of you complain, I finished it. It works all I want is something that is in the back of my head bugging me. If a user was to input a letter instead of a number how do I keep the program from crashing and instead print an error? I've tried google and I've tried looking around.

import java.util.Scanner;

public class Circle
{
	public static void main(String[] arg)
	{
		//create new scanner
		Scanner input = new Scanner(System.in);
		
		//declare variables
		double radius;
		double diameter;
		double circum;
		double pi = Math.PI;
		
		//Ask the question
		System.out.print("Please Enter a Radius: ");
		
		//create add proper data/calculations to variables
		radius = input.nextDouble();
		diameter = 2*radius;
		circum = 2*(pi*radius);
		
		System.out.println("Diameter: "+diameter);
		System.out.println("Circumference: "+circum);
	}
}

Recommended Answers

All 5 Replies

Nevermind I got it from this tutorial:
http://www.deitel.com/articles/java_tutorials/20060408/Arithmetic_InputMismatch_Exceptions/

import java.util.InputMismatchException;
import java.util.Scanner;

public class Circle
{
	public static void main(String[] arg)
	{
		//create new scanner
		Scanner input = new Scanner(System.in);
		
		//declare variables
		double radius;
		double diameter;
		double circum;
		double pi = Math.PI;
		
		//Ask the question
		System.out.print("Please Enter a Radius: ");
		
		
		//create add proper data/calculations to variables
		try
		{
			radius = input.nextDouble();
			diameter = 2*radius;
			circum = 2*(pi*radius);
		
			System.out.println("Diameter: "+diameter);
			System.out.println("Circumference: "+circum);
		}
		catch(InputMismatchException inputMismatchException)
		{
			System.out.println("Did you enter a number?\nRestart Program!");
		}
	}
}

Nevermind I got it from this tutorial:
http://www.deitel.com/articles/java_tutorials/20060408/Arithmetic_InputMismatch_Exceptions/

//create add proper data/calculations to variables
		try
		{
			radius = input.nextDouble();
		}
		catch(InputMismatchException inputMismatchException)
		{
			
}

I believe it is better to read them as Strings and then do the converting.
What will happen if the user enters: >" 2123 "

If you read it as String then:

int i = Integer.parseInt(input.trim())

Thanx but my Scanner forcefully(by assignment) had to be nextDouble but something I really have to read up on is using try/catch method and their errors/exemptions

Just to add: you can use the alternative way (a bit longer, but still can be useful some day :) ). For the case, if you don't want to interrupt your prog when the user inputs the incorrect value. You can design a method, using Class Character. This class contains method isDigit(), that can help you check, whether the given character is Digit.
Then you can do whatever you want (in case if user input incorrect value):
1. Replace the value, entered by user by default value.
2. Remove all incorrect symbols from it and return the proper value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.