• Post Reply Bookmark Topic Watch Topic
  • New Topic

Taking input from a text file.  RSS feed

 
Jeremiah Parrack
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my program, I am creating a weighted directed graph. I have done this hard coding the values now I need to get input from a text file.

This is how I'm able to create the graph hardcoding the values. 



Node 0 is connected to 2 with a weight of 1, then node 0 is connected to node 3 with a weight of 4  . . .

The text file I am grabbing from is





With n= the number of nodes and m = number of edges.
What i want to do is this.


I do not know how to do approach this. my initial way was to just put a loop inside of the Edge[]  but you cannot do that. I am just really lost since the values I get must be stored in an Edge[]. Thank you for any help. If there are typos please excuse them I have been working on this for 2 days without much sleep.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gets a bit tricky when you have two different line formats you have to detect. One possible way of dealing with this is to have a Scanner for the file and use hasNextLine() / nextLine() to get a line. Then create a new Scanner for the String returned by nextLine(). The String Scanner can then detect whether it has one or two int values using hasInt() & nextInt().
 
Jeremiah Parrack
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:It gets a bit tricky when you have two different line formats you have to detect. One possible way of dealing with this is to have a Scanner for the file and use hasNextLine() / nextLine() to get a line. Then create a new Scanner for the String returned by nextLine(). The String Scanner can then detect whether it has one or two int values using hasInt() & nextInt().



So after woking on it for awhile i came up with this which prints out if its a node or an edge.


So now I have to save this node as a var then add the edge and weight to the Edge[]. So once I am able to get my source node, edge and weight how to I put it in the Edge[] ?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest replacing the while() loop on line 11 with two discrete if( scanner.hasNextInt() ) lines. You will always find one int which maybe the node number or may be the first vertex of an edge, depending on the results of the next hasNextInt() call.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also need to work out what you are doing with values returned. What will happen to the value in line 13? What would happen if you reinstated line 14? Why have you called the variable lineScanner?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!