Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Operation on a txt file..

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Operation on a txt file..

    1st is to obtain data from this text file n simply show in the console n then to show the information separated by commas under the columns of name: roll number: class: phone number: course:

    secondly it's to tell that how much characters, integers, blank spaces are in the .txt file...

    FILE DATA:

    andre neil, 211, IT, 04130001701, BSCS
    David miller, 219, CS, 0234000201, BSCS
    Johnson, 344, Maths, 0523400000478, BS(Maths)


    import java.util.*;
    import javax.swing.*;
    import java.io.*;
    public class TestStudent{
     
    public static void main(String args[]){
     
     
    	FileReader fr  = null;
     
    	BufferedReader br = null;
          String[] tokens=null;
     
    try{
     
     
    	fr = new FileReader("student.txt");
    	br = new BufferedReader(fr);
      	String line = br.readLine();
      	while (line !=null)
    	        {
     
     
    				 tokens= line.split(",");
      	          System.out.println(line);
      	           line = br.readLine();
     
    	         }
     
    	br.close();
    	fr.close();
     
    	}
              catch(IOException ioex){
    	      System.out.println(ioex);
     
    	}
     
     
     
    System.out.println("student no.1");
    Student s1 =new Student(tokens[0],tokens[1], tokens[2], tokens[3], tokens[4]);
    s1.print();
     
     
    }

    it's giving me just the last student's data

    student class is another file just to show the columns headings accordingly...


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Location
    Germany
    Posts
    20
    My Mood
    Bored
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Operation on a txt file..

    The problem is when you read the data into tokens[].
    while (line !=null)
    {
    	tokens= line.split(",");
    	System.out.println(line);
    	line = br.readLine();
    }

    'Tokens' can only contain data of just one student.
    So in the loop 'tokens' is overwritten with other data all the time.

Similar Threads

  1. Java File Operation
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: March 28th, 2011, 07:54 AM
  2. Overwriting txt file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 08:21 AM
  3. [SOLVED] parse txt file
    By maliv in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: November 17th, 2010, 03:54 PM
  4. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  5. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM

Tags for this Thread