1 Replies - 504 Views - Last Post: 03 March 2012 - 09:28 AM Rate Topic: -----

#1 Seham-h   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-March 12

plz help me how to read text from txt file and put the data into list

Posted 03 March 2012 - 09:22 AM

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Student {
	
 private Node first,last;
 private int count;
    public Student(String name) throws FileNotFoundException
    {
    	
    	File sourceFile=new File(name);
    	
        Scanner input= new Scanner(sourceFile);
    Object list=new LinkedList();
    	StringBuffer temp=new StringBuffer ();
    	while (input.hasNext())
    	{
    		temp.append(input.nextLine());
    		list = temp.toString();
    	}
    }]

This post has been edited by blackcompe: 03 March 2012 - 09:23 AM
Reason for edit:: Please use [code] tags when posting to the forum.


Is This A Good Question/Topic? 0
  • +

Replies To: plz help me how to read text from txt file and put the data into list

#2 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: plz help me how to read text from txt file and put the data into list

Posted 03 March 2012 - 09:28 AM

list = temp.toString();


A string buffer and list are differing types so that's incorrect. If you want to create a list whose elements contain the lines from the files it's:

list.add(input.nextLine());



You should be using generic lists too.

 private Node first,last;
 private int count



What are they doing in the student class? It's looks like you're thinking of implementing a linked list. If so, those attributes don't belong there. You need to give a general overview of what the student class should do, because it looks like your design is headed in the wrong direction.

This post has been edited by blackcompe: 03 March 2012 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1