6 Replies - 317 Views - Last Post: 07 October 2012 - 09:26 AM Rate Topic: -----

#1 Gisengryl  Icon User is online

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 126
  • Joined: 08-September 12

Confusion between float/double values and nextDouble .

Posted 07 October 2012 - 08:23 AM

public static void readMark(){
		Scanner sc = new Scanner(System.in);
		float mark;
		System.out.print("Enter mark : ");
		mark = sc.nextDouble();
		System.out.println("Mark entered is " + mark);
	}


In this case, the compiler will state the error of "Type mismatch: Cannot convert double to float."

I don't understand.

From the java documentation, "Scans the next token of the input as a double... then the token is converted into a double value" .

So shouldn't the float value of the variable 'mark' be converted to a double value when it is scanned and not the other way round ?

Is This A Good Question/Topic? 0
  • +

Replies To: Confusion between float/double values and nextDouble .

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Confusion between float/double values and nextDouble .

Posted 07 October 2012 - 08:42 AM

No at the moment you are trying to put a double value into a float variable - thus the compiler is moaning at you. It's really no different to trying to store a String in an int variable, they are just both numeric types.

You can however type cast into a float if you want - mark = (float) sc.nextDouble();

or even better just change mark into a double variable.
Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Confusion between float/double values and nextDouble .

Posted 07 October 2012 - 08:46 AM

Java does not support changing the reference type at runtime

You can either convert the double value to a float, or change the reference type to double
Was This Post Helpful? 1
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Confusion between float/double values and nextDouble .

Posted 07 October 2012 - 08:51 AM

Bit of confused terminology there CasiOo floats and doubles are primitive types not reference types.
Was This Post Helpful? 1
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Confusion between float/double values and nextDouble .

Posted 07 October 2012 - 08:58 AM

View PostRyano121, on 07 October 2012 - 03:51 PM, said:

Bit of confused terminology there CasiOo floats and doubles are primitive types not reference types.


That is correct ^^ sorry for that
Was This Post Helpful? 0
  • +
  • -

#6 Gisengryl  Icon User is online

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 126
  • Joined: 08-September 12

Re: Confusion between float/double values and nextDouble .

Posted 07 October 2012 - 09:21 AM

View PostRyano121, on 07 October 2012 - 08:42 AM, said:

No at the moment you are trying to put a double value into a float variable - thus the compiler is moaning at you. It's really no different to trying to store a String in an int variable, they are just both numeric types.

You can however type cast into a float if you want - mark = (float) sc.nextDouble();

or even better just change mark into a double variable.


Oh I see thanks alot. Do you mind correcting my new understanding of the flow of the nextDouble method ? =/

Hmm..
1) variable 'mark' is declared as a float primitive type.
2) User enters "22.5"
3) Scanner detects '22.5' and converts/classify it to a double primitive type.
4) When printing out the output, variable 'mark' is still classified as a float primitive type, so the error occurs ?
( from what I understand from Casioo's post about runtime )
Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Confusion between float/double values and nextDouble .

Posted 07 October 2012 - 09:26 AM

Yeah that's pretty much it.

It should however be pointed out that at the moment your program never actually runs so the printing out never gets attempted. The error occurs in the compilation process before execution. May just be your wording though.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1