Solved

get tab delimited data from txt file in java

Posted on 2009-05-10
8
500 Views
Last Modified: 2013-12-22
Hi,

I want to extract tab delimited data from a text file which consists of integers.
I wrote the code for it but not able to get each integer and next line in the text file.

I pasted my code as well as text file..

import java.io.*;
 

public class HeapSort {
 

	public void readData(String filename)  throws Exception {

  		BufferedReader bin = new BufferedReader(new FileReader(filename));

  		

  			String s;

  		

  			s = bin.readLine();

  			while((s) != null)

  			{

  				String[] input = s.split("\t");

    		

    		

  				if (input == null) 

  				{

  					System.err.println( "No data found in the data file: " + filename + "\n");

  				}
 

    		

  				for(int i=0;i<input.length;i++)

  				{

  					System.out.println(input[i]);

  				}

    		

  				s = bin.readLine();

  			} // end of while loop
 

  			

    		bin.close();
 

    		
 
 

	}	// End of function readData

	

	

	public static void main(String[] args) throws Exception

	{

		HeapSort h = new HeapSort();

		h.readData("C:\\Users\\nnnn\\workspace\\HeapSort\\data2.txt");

		

	}

	

}

Open in new window

data2.txt
0
Comment
Question by:mmonline
  • 5
  • 3
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24349226
What problem are you having?
0
 

Author Comment

by:mmonline
ID: 24349230
Actually, I am not getting any output from this code..

i donot know if my code is correct or not.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24349232
Try instead the following (your file is space-delimited - but the code i posted will work for any whitespace)
String[] input = s.split("\\s+");

Open in new window

0
 

Author Comment

by:mmonline
ID: 24349242
thank you so much.. I ran it and i am able to get first line of data but not all the lines in the text file..
I mean i donot know how to read the next line in the file
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mmonline
ID: 24349257
Hey.. I am sorry I got the output.. ur answer is correct
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24349267
You can tell what the command line one is by doing the following, and what Eclipse's is by looking at the .classpath file
    out.println("public class HelloWorld {");

    out.println("  public static void main(String args[]) {");

    out.println("    System.out.println(\"This is in another java file and here is the classpath\");");

    out.println("    System.out.println(System.getProperty(\"java.class.path\"));");

    out.println("  }");

    out.println("}");

    out.close();

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24349274
Oops sorry - posted that in the wrong Q
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24349497
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse argument 14 53
eclipse shortcuts 9 45
Java SE 8u111  Lot of stuff broke 11 52
reverse digits of a number using for loop 5 36
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now