Single linked list

how to read data from file and link them?

Page 1 of 1

4 Replies - 3118 Views - Last Post: 05 December 2009 - 09:36 AM Rate Topic: -----

#1 kool001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 09

Single linked list

Post icon  Posted 04 December 2009 - 09:26 PM

I tried to create an array of linked list using linkedlist. But i m nt able to read data from a text file. what method should i use to read from a file and store it in an array of type LinkedList? The line where i get the error is highlighted.
how do i read data from file and to link them??

My code is as follows:
 public void readInput()
   {
	 Scanner input = null;
	 try
	  { 
	   input = new Scanner(new FileInputStream("Test1.txt"));
		StringTokenizer st = new StringTokenizer("Test1.txt");
		String [] array = new String[10];
		
	   String vertex = input.next();
		System.out.println("# of Vertexs are "+vertex);
		
//		String[] edge = new String[10];

		LinkedList[] edge = new LinkedList[10];
	// this for loop is to avoid null references in the array. 
		for ( int i = 0; i < edge.length; i++ ) 
		 {
		  edge[i] = new LinkedList();
		System.out.println("linked list named edge "+edge[i]);
	   }
		 
		  // need to change the condition here. cant hardcode the value!!
		  for(int i=0;i<4;i++)
		   { 
			 array[i]=input.next();
			 System.out.println("Vertex " +array[i]);
			}
			
	   // Reading the edges from the file and linking them to the vertex
		String numEdge = input.next();
		System.out.println("# of edges" +numEdge);
		for(int i=0;i<=5;i++)
		   { 
								[COLOR="Red"]edge[i]=input.nextLine();[/COLOR]
			 System.out.println("edges are " +edge[i]);
			  
			}
			
			
	   // here i need to assign edges to the vertices..... using array of linked list.
				
		
		 }	 
	  
	  catch(FileNotFoundException e)
	  { 
	   System.out.println("The file couldnt be found");
		System.exit(0);
	  } 
	 catch(IOException e)
	  { 
	   System.out.println("Error reading from file");
		System.exit(0);
	  }   
	 }




The error i get is

AbsGraph.java:144: incompatible types
found   : java.lang.String
required: LinkedList
			 edge[i]=input.nextLine();
								   ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.




Could anybody help??

Is This A Good Question/Topic? 0
  • +

Replies To: Single linked list

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Single linked list

Posted 04 December 2009 - 11:54 PM

edge is an array of LinkedList
edge[i] = new LinkedList();

you try to assign a String to it

edge[i]=input.nextLine();
Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3568
  • View blog
  • Posts: 16,265
  • Joined: 20-September 08

Re: Single linked list

Posted 05 December 2009 - 03:58 AM

There's something fundamentally wrong with your approach. Please post the data file
Was This Post Helpful? 0
  • +
  • -

#4 kool001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 09

Re: Single linked list

Posted 05 December 2009 - 08:43 AM

View Postg00se, on 5 Dec, 2009 - 02:58 AM, said:

There's something fundamentally wrong with your approach. Please post the data file


the data file is attached.

Attached File(s)

  • Attached File  data.txt (72bytes)
    Number of downloads: 105

Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3568
  • View blog
  • Posts: 16,265
  • Joined: 20-September 08

Re: Single linked list

Posted 05 December 2009 - 09:36 AM

LinkedList[] is not nearly as appropriate as List<LinkedList> for this type of thing. I suggest you use that and do it with a Scanner. You can fill each List with type String and then parse for digits later

This post has been edited by g00se: 05 December 2009 - 09:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1