Skip to content
Advertisement

Reading in a .txt file in Java

I am trying to write a program which reads in a a list of nodes that interact with each other within a network. This is written in a text file in the format:

JavaScript

This indicates node1 interacts with node2 and node3, node 2 interacts with only node3 etc.

The program will be able to read in this file and will remove any repeated interactions and will be able to return to me the number of interactions a node has with other nodes if I input the name of the node. However, I am very new to Java and am first trying to get it to read in the file, although my code does not read in the file at the moment. Here is the code I have so far:

JavaScript

Any help on how to solve this would be really appreciated, thank you!

Advertisement

Answer

Here is a full running answer if you are still interested…

For input file:

JavaScript

The filtered output:

JavaScript

SHORT ANSWER: Use HashMap<String, String> and

check repeating nodes with x_nodes.containsKey(x_key) && x_nodes.containsValue(x_value)

DETAILED ANSWER: Copy & paste the following source:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement