Using Scanner to Read in a Graph.txt

#i

  • D.I.C Caput

Reputation: 2

  • View blog
  • Posts: 182
  • Joined: 05-March 09

Reading a text file for a graph

Posted 02 May 2022 - 02:07 PM

I'chiliad trying to print the number of vertices in a text file on a graph which is the first integer in the file and then the blazon of graph is an undirected graph for a 0 or a 1 for a directed graph. Correct at present I'm just trying to print the number of vertices which is a 6 in the text file
Here'south the text file:
half dozen,1
0,2 0,4 1,4 i,five 2,one 2,iii 2,v 3,2 3,four four,1 4,5 5,ane 5,3 5,2
Im getting the following error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6, Size: 0
at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at Vertices.main(Vertices.java:21)

And then I need to know how to become about reading the next line in the file, do you read information technology with the nextLine
and put it into an arrayList or practise I read each alphabetize in the graph?

                public class Vertices         { 	public static void main(String[] args) throws Exception  	{ 	     		Scanner inFile = new Scanner(new File("graphs.txt")); 	    // Read the number of vertices 	    Cord line = inFile.nextLine(); 	    ArrayList<Integer> list=new ArrayList<Integer>(); 	    Cord[] information=line.carve up("[\\,]"); 	    int numberofvertices=Integer.parseInt(data[0]); 	    int typeOfGraph=Integer.parseInt(data[1]); 	    listing.add(numberofvertices,typeOfGraph); 	    System.out.println("The number of vertices is " + numberofvertices); } }            

This post has been edited by custurd122000: 02 May 2022 - 02:09 PM


Is This A Good Question/Topic? 0

  • +

#2 macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Reading a text file for a graph

Posted 02 May 2022 - 02:08 PM

Your indexing starts at 0. So if there are 2 elements, your indices are 0 and 1.

#iii custurd122000 User is offline

  • D.I.C Caput

Reputation: 2

  • View blog
  • Posts: 182
  • Joined: 05-March 09

Re: Reading a text file for a graph

Posted 02 May 2022 - 02:23 PM

Yes I merely seen that, thanks...I figured out the error now I just had a question on the next line in the file, I'm trying to read the next Line and each edge consists of two vertex identifi ers separated past a comma. Each border is separated past a infinite. So I read the adjacent Line and then how would I get about adding it? Do I ignore the spaces or add information technology into the ArrayList?

while(inFile.hasNext()) 	    { 	    	line=inFile.nextLine(); 	    	String[] data2=line.separate("[\\, ]"); 	    	//list.add() 	    }            

#4 macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Reading a text file for a graph

Posted 02 May 2022 - 02:24 PM

I'd probably split() the line on whitespace. Then you have edge pairs in each array element. It should exist fairly straight-forward to parse a String of length three.

#5 custurd122000 User is offline

  • D.I.C Caput

Reputation: 2

  • View blog
  • Posts: 182
  • Joined: 05-March 09

Re: Reading a text file for a graph

Posted 02 May 2022 - 02:44 PM

I was over thinking it, I just counted the number of commas in the next line and that gives me how many edges are in that location, I may have more than questions soon thank you for the assist

This postal service has been edited by custurd122000: 02 May 2022 - 02:58 PM

#half-dozen custurd122000 User is offline

  • D.I.C Head

Reputation: 2

  • View blog
  • Posts: 182
  • Joined: 05-March 09

Re: Reading a text file for a graph

Posted 02 May 2022 - 03:53 PM

Okay I take a question, if I want to print the first vertex before the comma and and then the second vertex afterwards the comma, how would I go about doing that? I know there's 14 vertices, then perchance a for loop? Then an index? I'yard not exactly certain.

#7 macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Reading a text file for a graph

Posted 02 May 2022 - 04:14 PM

Information technology sounds similar you lot only want to iterate over the tokens array later on y'all split() the line with the edges.

#8 custurd122000 User is offline

  • D.I.C Head

Reputation: ii

  • View blog
  • Posts: 182
  • Joined: 05-March 09

Re: Reading a text file for a graph

Posted 02 May 2022 - 04:22 PM

I did it ! Thank you, okay i more question I need to print out the in degree and out degree so if this is the lawmaking for printing out the vertices then j is the start vertex and i is the 2nd and so similar 0,2 is the first,
So vertices out degree 0 goes to 2 and in degree is 2 goes to 0 correct? How would I implement that?

              String[] data2=line.divide("[\\:]"); 	        Cord[][] vertices = new Cord[ data2.length ][]; 	        for (int i = 0; i < data2.length; i++){ 	        	vertices[i] = data2[i].separate("[ ]"); 	        } 	         	        System.out.println("Graph: "); 	        for (int j = 0; j < vertices.length; j++){ 	        	for (int i = 0; i < vertices[j].length; i++){ 	 	        	System.out.print("Alphabetize "+i+": "+vertices[j][i]+" \northward"); 	 	        } 	        	System.out.print("\n"); 	        }            

This post has been edited past custurd122000: 02 May 2022 - 04:31 PM

#9 custurd122000 User is offline

  • D.I.C Head

Reputation: 2

  • View blog
  • Posts: 182
  • Joined: 05-March 09

Re: Reading a text file for a graph

Posted 02 May 2022 - 05:28 PM

Anyone?

#10 macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-Dec 08

Re: Reading a text file for a graph

Posted 02 May 2022 - 06:17 PM

Delight avoid needlessly bumping your thread. You posted roughly an hour and a half ago.

The degree of a vertex is the number of edges incident to said vertex. The in-degree deals with the number of incoming arcs on a vertex, and the out-caste is the number of outgoing arcs on said vertex. So with this definition in mind, give it a endeavour.

#11 custurd122000 User is offline

  • D.I.C Caput

Reputation: 2

  • View blog
  • Posts: 182
  • Joined: 05-March 09

Re: Reading a text file for a graph

Posted 03 May 2022 - 10:32 AM

I have a few problems with my lawmaking...for the adjLists its supposed to calculate the order of the graph which is the beginning line in the file only it prints
[LVertex;@b988a6
Also getting the following error:
coffee.lang.ArrayIndexOutOfBoundsException: -1
at Graph.<init>(Graph.coffee:58)
at Graph.main(Graph.coffee:94)

"graphs.txt"
6,ane
0,2 0,4 one,four 1,5 2,1 2,iii 2,five 3,two 3,4 four,one 4,5 5,i v,3 five,two

              import coffee.io.File; import coffee.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner;   class Neighbor {     public int vertexNum;     public Neighbor next;     public Neighbor(int vnum, Neighbour nbr) {             this.vertexNum = vnum;             next = nbr;     } }   class Vertex {     String name;     Neighbor adjList;     Vertex(String name, Neighbor neighbors) {             this.proper name = name;             this.adjList = neighbors;     } }  public class Graph {       Vertex[] adjLists;           public Graph(Cord file) throws FileNotFoundException {                   Scanner sc = new Scanner(new File("graphs.txt"));         String line = sc.nextLine(); 	    Cord[] data=line.split("[\\,]"); 	            adjLists=new Vertex[Integer.parseInt(information[0])];         int graphType=Integer.parseInt(data[1]);                System.out.println("The order of the graph = " + adjLists);         // read edges         while (sc.hasNext()) {                           // read vertex names and translate to vertex numbers             int v1 = indexForName(sc.next());             int v2 = indexForName(sc.next());                           // add v2 to forepart of v1's adjacency list and             // add v1 to forepart of v2's adjacency list             adjLists[v1].adjList = new Neighbor(v2, adjLists[v1].adjList);                            adjLists[v2].adjList = new Neighbor(v1, adjLists[v2].adjList);             }         }           int indexForName(String proper name) {         for (int v=0; v < adjLists.length; five++) {             if (adjLists[v].name.equals(name)) {                 return five;             }         }         return -1;     }              public void impress() {         Organisation.out.println();         for (int five=0; v < adjLists.length; v++) {             System.out.print(adjLists[v].proper name);             for (Neighbor nbr=adjLists[v].adjList; nbr != null;nbr=nbr.next) {                 System.out.print(" --> " + adjLists[nbr.vertexNum].name);             }             Organisation.out.println("\north");         }     }           /**      * @param args      */     public static void principal(String[] args)      throws IOException {         // TODO Auto-generated method stub         Scanner sc = new Scanner(new File("graphs.txt"));          String file = sc.nextLine();         Graph graph = new Graph(file);         graph.print();       }   }            

#12 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: twenty-September 08

Re: Reading a text file for a graph

Posted 03 May 2022 - x:47 AM

Quote

Graph graph = new Graph(file);              

Your code is confusing. Why do you laissez passer 'something' to the ctor of Graph and and then proceed to ignore information technology in that ctor?

#13 macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Reading a text file for a graph

Posted 03 May 2022 - 02:35 PM

Please avert duplicate posting. Duplicate threads merged.

thomaswoulter.blogspot.com

Source: https://www.dreamincode.net/forums/topic/346184-reading-a-text-file-for-a-graph/

Comments

Popular posts from this blog

5 Best Chicago Neighborhoods for Black Singles, Young Professionals, & Families - Illinois

Gaps in Research for Future Self Continuity