Unreachable statement?? -- Error code help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EmilyA
    New Member
    • Aug 2008
    • 2

    Unreachable statement?? -- Error code help!

    Hi! I'm not that good at programming, so I was wondering if anyone could tell me why I'm getting an unreachable statement error for lines 18, 35, 68, and 86.

    Code:
    import java.util.ArrayList;
    
    public class Graph {
    	
    	ArrayList<ArrayList<Integer>> graph;
    	
    	public void Graph( int n )
    	{
    		graph = new ArrayList<ArrayList<Integer>>(n);
    	}
    	
    	public boolean connect( int add1, int add2 )
    	{
    		if( add1 >= graph.size() || add2 >= graph.size() 
    	            || add1 == add2 )
    		{
    			throw new IllegalArgumentException();
    			return false;
    		} else if( graph.get(add1).contains( new Integer(add2) ) ||
    			   graph.get(add2).contains( new Integer(add1) ) )
    		{
    			return false;
    		} else {
    			graph.get(add1).add( new Integer(add2) );
    			graph.get(add2).add( new Integer(add1) );
    			return true;
    		}
    	}
    	
    	public boolean disconnect( int dis1, int dis2 )
    	{
    		if( dis1 >= graph.size() || dis2 >= graph.size() )
    		{
    			throw new IllegalArgumentException();
    			return false;
    		} else if( !graph.get(dis1).contains( new Integer(dis2) ) ||
    			   !graph.get(dis2).contains( new Integer(dis1) ) )
    		{
    			return false;
    		} else {
    			graph.get(dis1).remove( new Integer(dis2) );
    			graph.get(dis2).remove( new Integer(dis1) );
    			return true;
    		}
    	}
    	
    	public boolean areConnected( int adj1, int adj2 )
    	{
    		if( adj1 >= graph.size() || adj2 >= graph.size() )
    		{
    			throw new IllegalArgumentException();
    		}
    		
    		if( graph.get(adj1).contains( new Integer(adj2) ) &&
    		    graph.get(adj2).contains( new Integer(adj1) ) )
    		{
    			return true;
    		} else {
    			return false;
    		}
    	}
    	
    	public ArrayList<Integer> adjacencies( int node )
    	{
    		if( node >= graph.size() )
    		{
    			throw new IllegalArgumentException();
    			return null;
    		}
    		
    		ArrayList<Integer> hold = new ArrayList<Integer>(graph.size());
    		
    		for( int i = 0; i > graph.get(node).size(); i++ )
    		{
    			hold.set(i,graph.get(node).get(i));
    		}
    		
    		return hold;
    	}
    	
    	public int degree( int node )
    	{
    		if( node >= graph.size() )
    		{
    			throw new IllegalArgumentException();
    			return 0;
    		}
    		
    		return graph.get(node).size();
    	}
    	
    	public boolean isEmpty()
    	{
    		return graph.size() == 0;
    	}
    }


    Any input is greatly appreciated!
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Take a look at lines 17 and 18:

    [CODE=Java]throw new IllegalArgument Exception();//17
    return false;//18[/CODE]

    Line 18 will never be executed because line 17 throws an exception, so 18 is unreachable. Solution: delete the unneeded line 18!

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by BigDaddyLH
      Take a look at lines 17 and 18:

      [CODE=Java]throw new IllegalArgument Exception();//17
      return false;//18[/CODE]

      Line 18 will never be executed because line 17 throws an exception, so 18 is unreachable. Solution: delete the unneeded line 18!
      Same for the other lines - throwing an Exception leaves the current function just like return would.

      Instead, you'll want to do something like this:[code=java]public void something()
      {
      ...
      boolean i;
      try {
      i = myFunction();
      } catch(WhateverE xception we) {
      i = false;
      }
      ...
      }

      public boolean myFunction() throws WhateverExcepti on
      {
      ...
      if(error) throw new WhateverExcepti on;
      else return true;
      }[/code]Greetings,
      Nepomuk

      Comment

      • EmilyA
        New Member
        • Aug 2008
        • 2

        #4
        Thank you so much!!!!

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by EmilyA
          Thank you so much!!!!
          You're welcome, EmilyA! And as we're at this anyway, welcome to bytes.com!

          It's great to have you here!

          When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags.

          Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!

          Greetings,
          Nepomuk

          Comment

          Working...