Incompatible types for returned object

I have a function from the Sedgewick's book

// return the shortest path from v to s as an Iterable
    public Iterable<String> pathTo(String v) {
        Stack<String> path = new Stack<String>();
        while (v != null && dist.contains(v)) {
            path.push(v);
            v = prev.get(v);
        }
        return path;
    }

When I compile the whole program, I always got error:

PathFinder.java:78: error: incompatible types
        return path;
               ^
  required: Iterable<String>
  found:    Stack<String>

The book website does not mention this bug. Quite vague with Iterable in Java so far. Can someone help me fix the problem? Thanks in advance.

It's not a bug. You will have to download and compile all the dependencies before compiling PathFinder.java - the dependencies are mentioned at the top blurb of each Java file. Also ensure the dependencies are in your CLASSPATH.

I tested the following using javac version 1.8 and 1.7 and they work fine with both.

Z:\data>
Z:\data>dir /b *
Graph.java
In.java
PathFinder.java
Queue.java
routes.txt
SET.java
ST.java
Stack.java
StdIn.java
StdOut.java
  
Z:\data>javac -version
javac 1.8.0_91
  
Z:\data>java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
  
Z:\data>javac StdOut.java
  
Z:\data>javac StdIn.java
  
Z:\data>javac In.java
  
Z:\data>javac SET.java
  
Z:\data>javac ST.java
  
Z:\data>javac Stack.java
  
Z:\data>javac Queue.java
  
Z:\data>javac Graph.java
  
Z:\data>javac PathFinder.java
  
Z:\data>type routes.txt
JFK MCO
ORD DEN
ORD HOU
ATL MCO
ORD HOU
DFW PHX
JFK ATL
ORD DFW
ORD PHX
ATL HOU
DEN PHX
PHX LAX
JFK ORD
DEN LAS
DFW HOU
ORD ATL
LAS LAX
  
Z:\data>java PathFinder routes.txt " " JFK
LAX
   JFK
   ORD
   PHX
   LAX
distance 3
MCO
   JFK
   MCO
distance 1
DFW
   JFK
   ORD
   DFW
distance 2
  
Z:\data>
Z:\data>

After cleaning up the .class files and setting up javac version 1.7:

Z:\data>
Z:\data>dir /b *
Graph.java
In.java
PathFinder.java
Queue.java
routes.txt
SET.java
ST.java
Stack.java
StdIn.java
StdOut.java
  
Z:\data>javac -version
javac 1.7.0_55
  
Z:\data>java -version
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) Client VM (build 24.55-b03, mixed mode, sharing)
  
Z:\data>javac StdOut.java
 
Z:\data>javac StdIn.java
  
Z:\data>javac In.java
  
Z:\data>javac SET.java
  
Z:\data>javac ST.java
  
Z:\data>javac Stack.java
  
Z:\data>javac Queue.java
  
Z:\data>javac Graph.java
  
Z:\data>javac PathFinder.java
  
Z:\data>type routes.txt
JFK MCO
ORD DEN
ORD HOU
ATL MCO
ORD HOU
DFW PHX
JFK ATL
ORD DFW
ORD PHX
ATL HOU
DEN PHX
PHX LAX
JFK ORD
DEN LAS
DFW HOU
ORD ATL
LAS LAX
  
Z:\data>java PathFinder routes.txt " " JFK
LAX
   JFK
   ORD
   PHX
   LAX
distance 3
MCO
   JFK
   MCO
distance 1
DFW
   JFK
   ORD
   DFW
distance 2
  
Z:\data>
Z:\data>

Instead of downloading and compiling the dependencies individually, you could, alternatively, download their jar file and put it in your CLASSPATH.

1 Like

Thank you so much for pointing out the CLASSPATH issue, which indeed is the problem!
I upgraded my java to 1.8.0_121 and extracted their library to a sub-folder I forgot. According to you clue I edited CLASSPATH and it seems working now .....