Switch statement with double

Is it possible to use a switch statement with a double?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 19887 Views - Last Post: 27 March 2009 - 07:22 PM Rate Topic: -----

#1 *noClue*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-February 09

Switch statement with double

Post icon  Posted 27 March 2009 - 05:37 PM

I keep getting this error:

C:\myJavaPgms\HwScore.java:52: possible loss of precision
found : double
required: int
switch (grade)
^
1 error

Tool completed with exit code 1

Is it possible to use the switch with a double?

Thanks for your help...

This post has been edited by *noClue*: 28 March 2009 - 12:51 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Switch statement with double

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Switch statement with double

Posted 27 March 2009 - 05:43 PM

Quote

...
14.11 The switch Statement
...
The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type (8.9), or a compile-time error occurs.
...


See The Java Language Specification, Third Edition

This post has been edited by n8wxs: 27 March 2009 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 *noClue*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-February 09

Re: Switch statement with double

Posted 27 March 2009 - 05:59 PM

View Postn8wxs, on 27 Mar, 2009 - 04:43 PM, said:

Quote

...
14.11 The switch Statement
...
The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type (8.9), or a compile-time error occurs.
...


See The Java Language Specification, Third Edition



Thanks for this information. I did read that and tried type casting the double as an integer but I guess I wasn't doing it properly.

I still get the same error. Any ideas?

This post has been edited by *noClue*: 28 March 2009 - 12:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Switch statement with double

Posted 27 March 2009 - 06:03 PM

i'm guessing grade is an instance variable?, if you have it declared as double then you will still have this problem. to solve this you will have to assign the value of the cast to an int and then pass it to the switch statement.

This post has been edited by mostyfriedman: 27 March 2009 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 *noClue*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-February 09

Re: Switch statement with double

Posted 27 March 2009 - 06:08 PM

View Postmostyfriedman, on 27 Mar, 2009 - 05:03 PM, said:

i'm guessing grade is an instance variable?, if you have it declared as double then you will still have this problem. to solve this you will have to assign the value of the cast to an int and then pass it to the switch statement.


Should it be an instance variable? I think I have it declared as a class variable.

This post has been edited by *noClue*: 28 March 2009 - 12:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Switch statement with double

Posted 27 March 2009 - 06:11 PM

switch (grade.charAt(0))


the charAt() method works with strings only, try this
switch ((int)grade)


Was This Post Helpful? 1
  • +
  • -

#7 *noClue*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-February 09

Re: Switch statement with double

Posted 27 March 2009 - 06:21 PM

That makes sense. It worked for the switch but the information within the switch is still not being returned. -1 is showing up when I run the program not the switch information.
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Switch statement with double

Posted 27 March 2009 - 06:26 PM

it depends on what will the value of "grade" be, its probably neither 1, 9, nor 8.that's why its returning -1
Was This Post Helpful? 0
  • +
  • -

#9 *noClue*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-February 09

Re: Switch statement with double

Posted 27 March 2009 - 06:31 PM

Doesn't the switch just look at the first character (case)? It is supposed to return the letter grade A for any average that equals 90 and up (the case 1 was to cover grades in the 100's), and letter grade of B for any average that equals 80-89 and so on. Am I doing it wrong?
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Switch statement with double

Posted 27 March 2009 - 06:35 PM

the case is somewhat equivalent to an if statement, here's a translation of your switch using if statements
if(grade == 9 || grade == 1)
	return this.A;

if(grade == 8)
	return this.B;

return -1;


get it now??
Was This Post Helpful? 0
  • +
  • -

#11 *noClue*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-February 09

Re: Switch statement with double

Posted 27 March 2009 - 06:39 PM

I do get it now...so an if statement would be more appropriate for my code since I'm needing it for a range of values?
Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Switch statement with double

Posted 27 March 2009 - 06:42 PM

not necessarily, you can still use a switch statement.
Was This Post Helpful? 0
  • +
  • -

#13 *noClue*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-February 09

Re: Switch statement with double

Posted 27 March 2009 - 06:45 PM

I prefer the switch statement because they look more streamlined but my text doesn't have a lot of examples of them other than with String.charAt. How can I use a switch for a range of values?
Was This Post Helpful? 0
  • +
  • -

#14 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Switch statement with double

Posted 27 March 2009 - 06:56 PM

if you want it to chec for a range of values then simply write the cases that belong to the range under eachother, for example
switch(int a)
{
	case 1:
	case 2: 
	case 3: System.out.println("hello");
	break;
	case 4:
	case 5: System.out.println("good bye");
	break;
}


this is equivalent to

if(a == 1 || a == 2 || a == 3)
	System.out.println("hello");
else 
if(a == 4 || a == 5)
	System.out.println("good bye");



get it now?
Was This Post Helpful? 1
  • +
  • -

#15 *noClue*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-February 09

Re: Switch statement with double

Posted 27 March 2009 - 06:59 PM

I guess I don't get it because if I need to do that for every value between 90-100 and 80-89 and so on that wouldn't be very efficient. Am I misunderstanding something. I was under the impression that the switch only checked for one value not a range of floating point values.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2