Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. read coulm from file and store it to array

read coulm from file and store it to array

Scheduled Pinned Locked Moved Java
helpjavadata-structurestutorial
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    newbie_
    wrote on last edited by
    #1

    Hello guys i am new to java and i have some problem that going to make my head explode i am stuck on this thing for a week now i need to read data file by column not by lines for example i have the following data file

    6, 148, 72, 35, 0, 336, 0627, 50, 2
    8, 183, 64, 0, 0, 233, 0672, 32, 2
    1, 85, 66, 29
    1, 89, 66, 23, 94, 281, 0167, 21, 1
    0, 137, 40, 35, 168, 431, 2288, 33, 2, 10, 9, 125
    5, 116, 74, 0, 0, 256, 0201, 30, 1

    i need to read it coulmn by coulmn and store it to arrays for exaample the first array would be {6,8,1,1,0,5} and so on all what i have done is read it line by line and thats not working for me this what i've done so far

    public class VariableTransformation
    {

    private String FileName = "";
    private FileReader FileReader;	
    private BufferedReader buffFile;
    private ArrayList<string\[\]> attrDomainList = new ArrayList<string\[\]>();	
    private String\[\] item=null;
    
    
    	
    
    
    public static void main(String\[\] args) 
    {
    	VariableTransformation tds = new VariableTransformation();
    	Scanner input=new Scanner(System.in);
    	System.out.println("===============================================");
    	System.out.print("Enter the file name>> ");
    	String filename;
    	filename=input.next();
    	tds.fileLoad(filename);
    	System.out.println("===============================================");
    		
    }
    
    public void fileLoad(String fileName)
    {
    	String line = "";
    	String\[\] attrDomain;
    	FileName = fileName;
    		
    	
    	System.out.println("> Reading File..");
    	try {
    		int lineCount=0;
    		int size=0;
    		FileReader = new FileReader(FileName);
    		buffFile = new BufferedReader(FileReader);
    
    		try {
    											
    			while((line = buffFile.readLine()) != null)
    			{
    					item = line.split(",");
    					size=item.length;
    					attrDomain=new String\[size\];
    					for (int i = 0; i < item.length; i++) {
    						attrDomain\[i\]=item\[i\];							
    					}						
    					attrDomainList.add(attrDomain);
    					lineCount++;
    			}
    			
    			printData(attrDomainList);
    			System.out.println("> Finish File Reading.");
    			System.out.println("Number of Lines>> "+lineCount);
    			System.out.println("===============================================");
    			
    		} 
    		catch (IOException e) {
    			System.out.println(e.getStackTrace().toString());
    			System.out.println("# Error while reading file .");
    		}
    		finally
    		{				
    			try {buffFile.close();} catch (IOException e) {e.printStackTrace();}
    			try {FileR
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups