Results 1 to 6 of 6
  1. #1
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Unhappy Strange unreachable statement error...

    hey guys, I've got a bizzare error here and it's got me stumped. Any and all input would be very helpful.

    Java Code:
    /*assign5.java - swaping values in an array
     *Gabriel Miller
     *12-10-10
     */
     
    public class assign5 //Declairing the public class
    {
    	public static void main(String[]args) //Begining the main method
    	{
    			 add(2, 3);
    			 System.out.println(" ");
    			 int[] numbers = {1, 2, 3, 4}; //Setting up the array
    			 printArray(numbers);
    			 System.out.println(" ");
    			 swap(numbers, 0, 1);
    			 printArray(numbers);
    			 System.out.println(" ");
    			 swap(numbers, 4, 5);
    			 swap(numbers, 0, 3);
    			 swap(numbers, 2, 1);
    			 swap(numbers, 2, 3);
    			 printArray(numbers);
    	}
    	
    	public static void add(int x, int y)
    	{
    		System.out.println(x + y);
    	}
    	
    	public static void printArray(int[] numbers)
    	{
    		 for (int i = 0; i < numbers.length; i++)
    		    {
    		        System.out.println(numbers[i]);
    		    }
    
    	}
    	
    	public static void swap(int[] numbers, int pos1, int pos2)
    	{
    		if ((pos1 > numbers.length) || (pos2 > numbers.length));
    		{
    			System.out.println("Invalid parameters.  Cannot compute.");
    			System.out.println(" ");
    			return;
    		}
    		
    		int n = 0;
    		int o = 0;
    		n = numbers[pos1];
    		o = numbers[pos2];
    		numbers[pos1] = o;
    		numbers[pos2] = n;
    		
    	}
    }
    When I try to compile it, it's giving me this error:
    [CODE]
    assign5.java:48: unreachable statement
    int n = 0;
    ^
    [\CODE]

    Any idea why it could be giving it to me? I thought I was just declaring a new int variable for temporary use... I need some help!!

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

    Default

    Java Code:
    if ((pos1 > numbers.length) || (pos2 > numbers.length)); // <-- because of the ; ...
    {
        System.out.println("Invalid parameters.  Cannot compute.");
        System.out.println(" ");
        return;
    }
    int n = 0; // <-- ...you never get here

    Basically the code says "if whatever then do nothing, then execute a block which ends in return, then assign 0 to n". You never get to the assignment because you always return inside the block.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    remove ; in line of if at swap method
    Java Code:
    	public static void swap(int[] numbers, int pos1, int pos2)
    	{
    		if ((pos1 > numbers.length) || (pos2 > numbers.length))
    		...
    		
    	}

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    EDIT: Did not see the last reply.

  5. #5
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Oh gosh... I never even noticed that I did that. Thanks guys.

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

Similar Threads

  1. Unreachable statement
    By lala in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 04:32 PM
  2. Strange Error
    By AJArmstron@aol.com in forum New To Java
    Replies: 1
    Last Post: 04-18-2010, 10:31 PM
  3. Unreachable statement Pig Dice Game
    By noturn10 in forum New To Java
    Replies: 2
    Last Post: 11-22-2009, 01:36 AM
  4. Replies: 6
    Last Post: 03-11-2009, 05:38 PM
  5. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 01:57 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
  •