DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    May 2005
    Posts
    115

    This shouldn't be...

    Please observe the following code
    Code:
    			Scanner input=new Scanner(System.in);
    
    			System.out.println();
    			System.out.println("Welcome to Breadth First Search TSP Solver");
    			System.out.println("==========================================");
    			System.out.println();
    
    			System.out.println("Please select preferred distance table:");
    			System.out.println("\t (1) Default distance table");
    			System.out.println("\t (2) User defined distance table");
    
    			int selectTable=input.nextInt();
    			System.out.println();
    
    			if(selectTable==1){
    				DistanceTable dt = DistanceTable.getDefault();
    			}else if(selectTable==2){
    				DistanceTable dt = DistanceTable.userTable("test.txt");
    			}else{
    				System.out.println("Invalid input. Program terminated.");
    			}
    
    			System.out.println("Please select preferred output destination:");
    			System.out.println("\t (1) Monitor screen");
    			System.out.println("\t (2) User defined .txt file");
    			System.out.print("Select 1 or 2: ");
    
    			int selectOutput=input.nextInt();
    			System.out.println();
    
    			BfsTSPSolver solver = new BfsTSPSolver();
    
    			TspIO io=new TspIO(selectOutput);
    			io.execute(dt, solver);
    Observe the last line io.execute(dt, solver);
    When i tried to compile it says that line can't find the symbol for dt...

    But isn't dt already defined in the middle with selectTable input??

    What am i missing here?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Variable scope basically:

    Here the scope of the two dt variables is inside the brackets.
    So, generally, you cannot code a conditional variable declaration and
    then use that variable outside the scope of the condition....
    Code:
    if(selectTable==1){
      DistanceTable dt = DistanceTable.getDefault(); // <-- "exists" only here
    }else if(selectTable==2){
      DistanceTable dt = DistanceTable.userTable("test.txt"); // <-- "exists" only here
    }else{
      System.out.println("Invalid input. Program terminated.");
    }
    Try this
    Code:
    DistanceTable dt=null;
    if(selectTable==1){
      dt = DistanceTable.getDefault();
    }else if(selectTable==2){
      dt = DistanceTable.userTable("test.txt");
    }else{
      System.out.println("Invalid input. Program terminated.");
    }
    And, just to be style-picky, the if statement should have been coded as a
    switch statement with two cases and a default.
    Last edited by sjalle; 09-03-2005 at 06:22 AM.
    eschew obfuscation

  3. #3
    Join Date
    May 2005
    Posts
    115
    Thx sjalle...

    1 more thing, would DistanceTable dt; instead of DistanceTable dt=null;

    both the same right? since dt would b initialized later ...

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Yes, dt will be null so the initialization in the declaration is redundant.
    I'm not sure if the declaration DistanceTable dt; gives initialization at compile
    time or during load though (and what would I do with that info ?)

    It's just an old habit of mine, it makes visual scanning of code easier, for me at least.
    eschew obfuscation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links