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 8948931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:07:12+00:00 2026-06-15T13:07:12+00:00

i have read file function which will read a txt file. after i read

  • 0

i have read file function which will read a txt file. after i read it, i put the value to a list. below is sample data:

public void readDisplayClient()
{   
DisplayClient dc = null;
try
{
    // Open the file that is the first 
    // command line parameter
    FileInputStream fstream = new FileInputStream("ClientData.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    String [] values = new String[3];
    int counter = 0;
    int clientCounter = 0;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   
    {
        // Print the content on the console

        String delims = ";";
        String[] tokens = strLine.split(delims);

        if(counter == 0)//Reading total clients
        {                       
            totalClient = Integer.parseInt(tokens[0]);
            counter++;
        }
        else
        {
            //System.out.println("Test " + counter + " " + tokens.length);
            for (int i = 0; i < tokens.length; i++)
            {
                    values[i] = tokens[i];
                    //System.out.println(tokens[i]);
            }
            dc = new DisplayClient(clientCounter,values[0],values[1],values[2]);
            //dc.printDetails(); // save the connected nodes details to logger txt file.
            clientList.add(dc);
            clientCounter++;
        }
    }
    //Close the input stream
    in.close();
    ss.setTotalClient(totalClient);
    ss.setClientList(clientList);
    //ss.printClientList();
}
catch (Exception e)
{//Catch exception if any
    System.err.println("Error: " + e.getMessage());
}
}

my txt data file will be something like :

2// total 2 conections

0;1;500; // Node 0 connect to Node 1 with 500 kbps

1;2;500 // Node 1 connect to Node 2 with 500 kbps

when node 1 is connected to node 2, it is actually also connect to node 0 as well. Is this able to put it on a hashmap ??

i am a bit confused on it. thanks in advance for help.

  • 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-06-15T13:07:13+00:00Added an answer on June 15, 2026 at 1:07 pm

    There are various ways to do it. Since each edge has a speed you can have a class for each node and a class for each edge:

    Create a class which represents your node. It should carry the data (node id), and which connections (edges) it has going out from it (since its a directed graph).

    public class Node
    {
      private int nodeId;
      private List<Connection> outboundConnections = new ArrayList<>();
    
      public Node(int nodeId)
      {
        this.nodeId = nodeId;
      }
    
      public void addConnection(Connection connection)
      {
        this.outboundConnections.add(connection);
      }
    
      //... setters and getters
    }
    

    Then create a class which represents the edges, including the data for the connection and which node it connects to (destination, since its a directed graph):

       public class Connection
       {
          private int speedKbps;
          private Node endNode;
    
          public Connection(Node endNode, int speedKbps)
          {
            this.endNode = endNode;
            this.speedKbps = speedKbps;
          }
    
          //... setters and getters
       }
    

    In your class you keep a Map of all the created nodes (best if it is a member of the class but depends on what you’re doing).

    Map<Integer, Node> nodes = new TreeMap<>(); 
    

    Then for each line in your loop you can do:

    int fromNodeId = new Integer(values[0]);
    int toNodeId = new Integer(values[1]);
    int speedKbps = new Integer(values[2]);
    
    Node fromNode = nodes.get(fromNodeId);
    if (fromNode == null) //if we haven't seen this node already, create it and add it to the map
    {
       fromNode = new Node(fromNodeId);
       nodes.put(fromNodeId, fromNode);
    }
    
    Node toNode = nodes.get(toNodeId);
    if (toNode == null) //if we haven't seen this node already, create it and add it to the map
    {
       toNode = new Node(toNodeId);
       nodes.put(fromNodeId, toNode);
    }
    
    Connection connection = new Connection(toNode, speedKbps);
    fromNode.addConnection(connection);
    

    This approach works for a directed graph, assuming you want to traverse from the one of the nodes in the direction of the arrows.

    There are of course other alternatives (for example storing it as a large 2D matrix, with the kbps as the number in the matrix and the node numbers on the left representing ‘from’ nodes, and the node numbers on the top the ‘to’ nodes, or the other way round).

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

Sidebar

Related Questions

When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I have a simple file read and write function. private void WriteToFile(String filename, String
My current situation is: I have to read a file and put the contents
in my application there is a small part of function,in which it will read
i have requirement to store the regex in flat file and read them.There will
I have a ListBox which I put some files, if the file is not
I have a FindFile routine in my program which will list files, but if
I will read a sequential file which include some string such as 79.85, 1000,
I am designing one application in which I have to read one file given
I have a text.txt file which contains following txt. Kontagent Announces Partnership with Global

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.