Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6818365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:10:10+00:00 2026-05-26T21:10:10+00:00

For this task I’m supposed to create a wannabe network that is supposed to

  • 0

For this task I’m supposed to create a “wannabe” network that is supposed to be a graph. The first file you read contains a textfile with vertices from/to and some other data. I have also an inner counter who counts how many times addVertex has been used. So far it’s correct and the test print is correct, but when I run it has no vertices in the list even tough it says it has been added.
Any ideas why id won’t add to it’s list and any ideas?

Here is how I read:

static Graph graph;

private static void createNetwork(String fil1) {
    try {
        Scanner sc = new Scanner(new File(fil1));
        graph = new Graph();

        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            String[] split = line.split("\t");
            int[] connections = new int[split.length];
            //  System.out.println(line); // test utskrift
            for (int i = 0; i < split.length; i++) {
                connections[i] = Integer.parseInt(split[i].trim());
            }
            graph.addVertex(connections);
        }
    } catch (Exception e) {

    }
}

And some other methods that is being called on:

public void addVertex(int[] cons) {//, int tid, int ore) {
    if (cons == null) {
        return;
    }
    boolean added = false;
    Vertex fra, til;
    int tid = cons[2];
    int ore = cons[3];

    fra = new Vertex(cons[0], cons[1], cons[2], cons[3]);
    til = new Vertex(cons[1], cons[0], cons[2], cons[3]);

    if (verticies.contains(fra) == false) { //, tid, ore)
        System.out.println(
                fra.id + " --> " + til.id + " Ble lagt til i lista! " + size);
        size++;
        added = verticies.add(fra); //, tid, ore
        //   addEdge(fra, til, tid, ore);
        //  addEdge(til, fra, tid, ore);
        // addBiEdges(fra, til, tid, ore);
        //  return true;
    }
}

public boolean addBiEdges(Vertex fra, Vertex til, int tid, int ore) throws IllegalArgumentException {
    return false; // addEdge(fra, til, tid, ore) && addEdge(til, fra, tid, ore);
}

public void addEdge(Vertex fra, Vertex til, int tid, int ore) throws IllegalArgumentException {
    if (verticies.contains(fra) == false)
        throw new IllegalArgumentException(fra.id + " er ikke med i grafen!");
    if (verticies.contains(til) == false)
        throw new IllegalArgumentException(til.id + " er ikke med i grafen!");

    Edge e = new Edge(fra, til, tid, ore);
    if (fra.findEdge(til) != null) {
        return;
    } else {
        fra.addEdges(e);
        til.addEdges(e);
        edges.add(e);
        // return true;
    }
}

class Graph {
    public static int size;
    HashMap<Integer, Vertex> graph;

    protected List<Vertex> verticies;
    protected List<Edge> edges;
    // Brukes til Dijkstras algoritmen
    public List<Vertex> kjent;
    public List<Vertex> ukjent;


    public Graph() {
        graph = new HashMap<Integer, Vertex>();
        kjent = new ArrayList<Vertex>();
        ukjent = new ArrayList<Vertex>();
        verticies = new ArrayList<Vertex>();
        edges = new ArrayList<Edge>();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:10:10+00:00Added an answer on May 26, 2026 at 9:10 pm

    They aren’t added to the list in the first place. addVertex() prints out the message about the vertex being added to the list, although it hasn’t done so, yet. Then it tries to but fails, resulting in an exception being thrown by ArrayList.add() The exception gets caught in createNetwork(), so you won’t notice something went wrong.

    Don’t catch exceptions you won’t handle. Don’t log actions before they have been carried out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please explain what this task is about? Create a generic linked list class that
I had this task to read a file, store each character in a dict
Not sure how to accomplish this task. I create a xml file from a
This task has already been asked/answered, but I recently had a job interview that
I have this task that creates a service: Target Name=InstallService DependsOnTargets=CopyFiles Exec Command=sc \
Hello I am solving this task Problem28 , I think that its a easy
I think this task is easier for Android emulator. We just create new images
Is this a 'real' task, that can be written on any language ( C/C++,
I'm kinda stuck with this task. I have a macro that retrieve two ranges
So I have this task to create a horizontal scrolling array of image buttons

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.