get tab delimited data from txt file in java

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
mmonlineAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
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
 
CEHJCommented:
What problem are you having?
0
 
mmonlineAuthor Commented:
Actually, I am not getting any output from this code..

i donot know if my code is correct or not.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mmonlineAuthor Commented:
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
 
mmonlineAuthor Commented:
Hey.. I am sorry I got the output.. ur answer is correct
0
 
CEHJCommented:
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
 
CEHJCommented:
Oops sorry - posted that in the wrong Q
0
 
CEHJCommented:
:-)
0
All Courses

From novice to tech pro — start learning today.