Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Graph class, ToString help.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Graph class, ToString help.

    Hello,

    I have an assignment that I'm working on, I have completed every thing but one part, the toString of the Graph Class. I am having a hard time creating my toString.

    The professor wants us to use both the vertexIterator and the outEdges methods in the Graph class to represent our toString. Here is the code I wrote in the Graph class.

        import java.util.*;
        public class Graph
       {
          private List<Edge> outEdge;
          private List<Vertex> verticies; 
          private List<Edge> edges;
          private int numberEdges;
          private int numberVertices;
     
     
           public Graph() 
          {
             numberVertices = 0;
             numberEdges = 0;
             verticies = new ArrayList<Vertex>();
             edges = new ArrayList<Edge>();
     
          }
           public void addVertex(Vertex v) 
          {
             numberVertices++;
             verticies.add(v);
             outEdge = v.getAdjacentList();
          }
           public void addEdge(Edge e) 
          {
             numberEdges++;
             edges.add(e);
             outEdge.add(e);
     
          }
     
           public int numVertices() 
          {
             return numberVertices;
          }
     
           public int numEdges()
          {
             return numberEdges;
          }
     
           public Iterator outEdges(Vertex v)
          {
             //outEdge = v.getAdjacentList();       
             return v.outEdges();
          }
     
           public Iterator vertexIterator()
          {
             return verticies.iterator();
          }
     
           public String toString()
          {
             StringBuffer tmp = new StringBuffer();
     
     
             for(Iterator vIterator = vertexIterator(); vIterator.hasNext(); )
                Vertex v = (Vertex) vIterator.next();   	   
             tmp.append("outgoing edges from vertex: ");
             for (Iterator it = v.outEdges(v); it.hasNext(); ) 
             {
                Edge s = (Edge)it.next(); 
                tmp.append(s);
             } 
             return tmp.toString();
          }
     
       }

    This is the error message I'm getting

    Graph.java:68: not a statement
    Vertex v = (Vertex) vIterator.next();
    ^
    Graph.java:68: ';' expected
    Vertex v = (Vertex) vIterator.next();
    ^
    2 errors
    This is literally the final part of my project. I don't know how to get the Vertex section correct. Any help will be greatly appreciated.

    If you need the rest of my code, I can provide it.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Graph class, ToString help.

    Hello johnjames!
    The {} are missing after the for loop.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    johnjames (April 28th, 2012)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Graph class, ToString help.

    Take note of the scope of the variables, and the usage (or lackthereof) of brackets in your for loop.

  5. The Following User Says Thank You to copeg For This Useful Post:

    johnjames (April 28th, 2012)

  6. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Graph class, ToString help.

    Thank You for your responses andreas90 and copeg! So simple. Now I gotta rework my toString code so it can print the proper output.

    If I have anymore questions about this toString, I will definitely ask in here.

Similar Threads

  1. Replies: 1
    Last Post: February 12th, 2012, 01:01 AM
  2. Replies: 1
    Last Post: February 11th, 2012, 07:38 AM
  3. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  4. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:37 AM
  5. [SOLVED] Using class implicit toString() for array index
    By Quetzalma in forum Java Theory & Questions
    Replies: 2
    Last Post: February 3rd, 2010, 05:04 PM