Results 1 to 2 of 2
  1. #1
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default 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. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.

Similar Threads

  1. about conditonal if else and switch case
    By niksipandit in forum New To Java
    Replies: 6
    Last Post: 09-22-2011, 12:53 PM
  2. java switch case
    By aconti in forum New To Java
    Replies: 16
    Last Post: 08-09-2011, 08:05 AM
  3. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 06:56 PM
  4. Replies: 2
    Last Post: 02-23-2009, 01:34 AM
  5. Switch Case and Key Events
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 01-27-2009, 12:20 AM

Posting Permissions

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