Thread: error when i use switch-case

1. Member
Join Date
Nov 2010
Posts
75
Rep Power
0

error when i use switch-case

Hi, i have written the following code and i got tons of errors, dont know why...please any guidance
Java Code:
```import java.lang.*;

public class TestSwitch
{
public static Double[] test(int i)// i should be from 1 to 4
{
Double []prob = new Double[4];
double x,y,z,x2,y2,z2;

Random rand = new Random();

switch(i)
{
case 1:
prob[i-1] = 25.0 + (rand.nextDouble(75) + 1);

x = 100 - prob[i-1];
y = x/3;
z =  y * 2;
prob[1]= y + rand.nextDouble(z);

x2 = x-prob[1];
y2 = x2/2;
prob[2] = y2 + rand.nextDouble(y2);

prob[3] = 100 - ( prob[i-1] + prob[1] + prob[2] );

break;

case 2:
prob[0] = 25 + rand.nextDouble(75) + 1;

x = 100 - prob[0];
y = x/3;
z =  y * 2;
prob[i-1] = y + rand.nextDouble(z);

x2 = x-prob[i-1];
y2 = x2/2;
prob[2] = y2 + rand.nextDouble(y2);

prob[3] = 100 - ( prob[0] + prob[i-1] + prob[2] );

break;

case 3:
prob[0] = 25 + rand.nextDouble(75) + 1;

x = 100 - prob[0];
y = x/3;
z =  y * 2;
prob[1]= y + rand.nextDouble(z);

x2 = x-prob[1];
y2 = x2/2;
prob[i-1] = y2 + rand.nextDouble(y2);

prob[3] = 100 - ( prob[0] + prob[1] + prob[i-1] );

break;

case 4:
prob[0] = 25 + rand.nextDouble(75) + 1;

x = 100 - prob[0];
y = x/3;
z =  y * 2;
prob[1]= y + rand.nextDouble(z);

x2 = x-prob[1];
y2 = x2/2;
prob[2] = y2 + rand.nextDouble(y2);

prob[i-1] = 100 - ( prob[0] + prob[1] + prob[2] );

break;
}
return prob;
}

public static void main (String[] args)
{
Double []t = TestSwitch.test(4);
System.out.println (t[1]);
}
}```
and here are the error messages:
Java Code:
```--------------------Configuration: <Default>--------------------
D:\3rd semster\Java\ProjectTest\TestSwitch.java:10: cannot find symbol
symbol  : class Random
location: class TestSwitch
Random rand = new Random();
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:10: cannot find symbol
symbol  : class Random
location: class TestSwitch
Random rand = new Random();
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:15: operator + cannot be applied to Random.nextDouble,int
prob[i-1] = 25.0 + (rand.nextDouble(75) + 1);
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:15: operator + cannot be applied to double,<nulltype>
prob[i-1] = 25.0 + (rand.nextDouble(75) + 1);
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:20: operator + cannot be applied to double,Random.nextDouble
prob[1]= y + rand.nextDouble(z);
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:24: operator + cannot be applied to double,Random.nextDouble
prob[2] = y2 + rand.nextDouble(y2);
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:31: operator + cannot be applied to int,Random.nextDouble
prob[0] = 25 + rand.nextDouble(75) + 1;
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:31: operator + cannot be applied to <nulltype>,int
prob[0] = 25 + rand.nextDouble(75) + 1;
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:36: operator + cannot be applied to double,Random.nextDouble
prob[i-1] = y + rand.nextDouble(z);
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:40: operator + cannot be applied to double,Random.nextDouble
prob[2] = y2 + rand.nextDouble(y2);
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:47: operator + cannot be applied to int,Random.nextDouble
prob[0] = 25 + rand.nextDouble(75) + 1;
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:47: operator + cannot be applied to <nulltype>,int
prob[0] = 25 + rand.nextDouble(75) + 1;
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:52: operator + cannot be applied to double,Random.nextDouble
prob[1]= y + rand.nextDouble(z);
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:56: operator + cannot be applied to double,Random.nextDouble
prob[i-1] = y2 + rand.nextDouble(y2);
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:63: operator + cannot be applied to int,Random.nextDouble
prob[0] = 25 + rand.nextDouble(75) + 1;
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:63: operator + cannot be applied to <nulltype>,int
prob[0] = 25 + rand.nextDouble(75) + 1;
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:68: operator + cannot be applied to double,Random.nextDouble
prob[1]= y + rand.nextDouble(z);
^
D:\3rd semster\Java\ProjectTest\TestSwitch.java:72: operator + cannot be applied to double,Random.nextDouble
prob[2] = y2 + rand.nextDouble(y2);
^
18 errors

Process completed.```

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

Re: error when i use switch-case

Java Code:
```D:\3rd semster\Java\ProjectTest\TestSwitch.java:10: cannot find symbol
symbol  : class Random
location: class TestSwitch
Random rand = new Random();```
The compiler cannot find the Random class, because you have not imported it. (Incidentally there is no need to import java.lang.*)

Frankly the other errors are reported in a rather mysterious way, but the bottom line is that you cannot meaningfully say "rand.nextDouble(75)". Read the nextDouble() API docs to see how this method should be used.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•