Java input variable type problem

Thread Starter

jakegwood

Joined Apr 13, 2011
29
Hello. I am doing a java excercise where I have to determine if two circles with input centers and radii intersect.

the form I use for inputing each parameter is:

Scanner input = new Scanner(System.in);

System.out.print("Please input the x coordinate of the first circle: ");
double x1 = input.nextDouble();
System.out.print("Please input the y coordinate of the first circle: ");
double y1 = input.nextDouble();

I have used all the necessary imports and declarations and the program compiles fine. It works well, but only if I input integers, however when I input a decimal point number, terminal returns:
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextDouble(Scanner.java:2335)
at threeDotTwentyNine.main(threeDotTwentyNine.java:10)
I have tried compiling this with float too and that returned a similar error.

Any suggestions on how to fix this?
 

kubeek

Joined Sep 20, 2005
5,795
First, post the whole code so we can see where exactly it fails.
Second, are you by any chance located in europe or other non-us country? Java is notorious with locales problems such as the difference between dot and comma used as a decimal point in different countries. IIRC there should exist some initialisation to get rid of this problem, but I never looked for it.

You should also use try-catch to trap and print out the error message form the nextDouble method.
 

Thread Starter

jakegwood

Joined Apr 13, 2011
29
Here's the entire code:

THAT IS SO WEIRD. I am in US, but I tried using a comma for a decimal place and it worked. Go figure. Anyway, I'd still like it to work normally.

import java.util.Scanner;
import java.lang.Math.*;

public class threeDotTwentyNine {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Please input the x coordinate of the first circle: ");
double x1 = input.nextDouble();
System.out.print("\nPlease input the y coordinate of the first circle: ");
double y1 = input.nextDouble();
System.out.print("\nPlease input the x coordinate of the second circle: ");
double x2 = input.nextDouble();
System.out.print("\nPlease input the y coordinate of the second circle: ");
double y2 = input.nextDouble();
System.out.print("\nPlease input the radius of the first circle: ");
double radius1 = input.nextDouble();
while (radius1<0)
{
System.out.print("\nPlease enter a non-negative number: ");
radius1 = input.nextDouble();
}
System.out.print("\nPlease input the radius of the second circle: ");
Double radius2 = input.nextDouble();
while (radius2<0)
{
System.out.print("\nPlease enter a non-negative number: ");
radius2 = input.nextDouble();
}

double distance = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
double radDif = Math.abs(radius1-radius2);

if (distance < radDif)
{
if (radius1>radius2)
{
System.out.println("\nThe second circle is inside the first.");
}
if (radius2>radius1)
{
System.out.println("\nThe first circle is inside the second.");
}
}
else if ((radDif<=distance) && ((radius1 + radius2)>=distance))
{
System.out.println("\nThe circles intersect.");
}
else
{
System.out.println("\nNeither circle is inside the other, nor do they intersect");
}
}
}
 

Thread Starter

jakegwood

Joined Apr 13, 2011
29
Thanks, I will take a look at that, but your previous idea fixed the problem.

When I wrote the program I was using an extended keyboard layout, which I assume affected the programmer or the compiler in some way. I switched it back and it worked.

Thanks!
 
Top