4 Replies - 7483 Views - Last Post: 11 February 2009 - 03:01 PM Rate Topic: -----

#1 TheOne6152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-February 09

Error with input.nextDouble();

Posted 11 February 2009 - 02:47 PM

I was wondering if anyone could help me with this error. I just started Java three days ago and i cant get why this error keeps comming up when i have this code.

ERROR:
----------------------Configuration: Rp82Distance2 - JDK version 1.6.0_11 <Default> - <Default>--------------------
C:\Users\XXXXXXXXX\Desktop\Rp82Distance2\src\Rp82Distance2.java:26: incompatible types
found   : double
required: java.lang.String
	runner = input.nextDouble();
							 ^
1 error

Process completed.



CODE:
import java.awt.*;
 import java.applet.*;
 import java.util.Scanner;
 
public class Rp82Distance2 {
	
	public static void main(String[] args) {
	
	double seg1;
	double seg2;
	double seg3;
	String runner;
	double distance;
	Scanner input = new Scanner(System.in); 
	
	System.out.print("Enter your name: ");
	runner = input.nextDouble();
	System.out.print("Enter race 1 distance: ");
	seg1 = input.nextDouble();
	System.out.print("Enter race 2 distance: ");
	seg2 = input.nextDouble();
	System.out.print("Enter race 3 distance: ");
	seg3 = input.nextDouble();
	distance = seg1 + seg2 + seg3;
	System.out.println(runner +"'s total race distance is " + distance + " km.");
	}
}


This post has been edited by TheOne6152: 11 February 2009 - 02:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Error with input.nextDouble();

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Error with input.nextDouble();

Posted 11 February 2009 - 02:49 PM

You have declared the variable runner to be of type String but you try to store a double, incompatible types for you.
Was This Post Helpful? 0
  • +
  • -

#3 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: Error with input.nextDouble();

Posted 11 February 2009 - 02:50 PM

Well, runner is a string not a double. input.next() gives you the input as a string.

edit: aargh, to late!

This post has been edited by LaFayette: 11 February 2009 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Error with input.nextDouble();

Posted 11 February 2009 - 02:51 PM

I'm guessing you actually wanna store a string so just do,
runner = input.next();
Was This Post Helpful? 0
  • +
  • -

#5 TheOne6152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 11-February 09

Re: Error with input.nextDouble();

Posted 11 February 2009 - 03:01 PM

Thank you for your very fas responces. it works now :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1