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 3 of 3

Thread: Generating array (or arraylist) matrix from txt file

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

    Default Generating array (or arraylist) matrix from txt file

    Hey guys, im trying to generate matrix to store info from a txt file. My code reads every line from the file, counts the number of lines,
    creates an array of that size. Then stores every line into a string array slot and splits each line by space character and creates another array of of it.
    then by doing two "for" cycles i insert the information into my matrix. Im not sure if it works or how to test it. i could use some help fixing the code
    and/or doing the same but with arraylists (i have no clue how to use them). I have to store information into a matrix the size of lines and then fill each column
    with the number of elements i find in my file when spliting a line. If someone can think of an alternative way of procesing my files id Thanks!

    PS: my nodos.txt file stores info as follows. Ex:

    4
    1 1
    3 2
    4 -3
    EOF




     
    public String[][] LectorNodos( ) throws IOException{
     
    			int LargoArchivo=0;
    			String [] infoNodos;
    			File HojaNodos=new File("nodos.txt");
    			String [][] matrizInfoNodos;
     
    			String [] aux;
     
     
    			if(HojaNodos.exists()){
     
    				Scanner lector = new Scanner(HojaNodos);
     
    				while(lector.hasNext()){
    					lector.nextLine();
    					LargoArchivo++;	
    				}
    				lector.close();
     
    				lector= new Scanner(HojaNodos);
    				infoNodos = new String [LargoArchivo];
     
     
    				for(int i=0;i<infoNodos.length;i++){
     
    					infoNodos[i]=lector.nextLine();
    				}
     
    				lector.close();
     
    				matrizInfoNodos=new String [LargoArchivo][3];
     
    				for(int k=0;k<infoNodos.length;k++){
     
    				aux=infoNodos[k].split("\\s+");
     
    					for(int j=0; j<aux.length;j++){
     
    						matrizInfoNodos[k][j]=aux[j];					
     
    					}
     
    				}
     
    				for(int i=0;i<matrizInfoNodos.length;i++){
     
    					for(int j=0;j<matrizInfoNodos[0].length;j++){
     
    						Usuario.mensaje(matrizInfoNodos[i][j]);
     
    					}
    				}
     
     
     
    				return matrizInfoNodos;
    			}
     
     
    			return null;
     
    		}
    Last edited by benjalizana; November 13th, 2011 at 02:44 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Generating array (or arraylist) matrix from txt file

    Hi benjalizana,
    here I post you my solution that has been tested ONLY with your file.
    You have to stress test it.

    ...edited by moderator

    Do you like this solution?
    Bye
    Last edited by copeg; November 14th, 2011 at 08:09 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Generating array (or arraylist) matrix from txt file

    lucasantos, please read the forum rules and the following article: http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. converting array into arraylist
    By Harry Blargle in forum Collections and Generics
    Replies: 6
    Last Post: October 30th, 2011, 03:38 AM
  2. Array List; How to pass objects into another arraylist
    By Melvrick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 05:55 AM
  3. [SOLVED] Problem copying an arraylist to an array
    By allhalf425 in forum Collections and Generics
    Replies: 4
    Last Post: September 20th, 2011, 10:25 AM
  4. Comparing elements of an arrayList with an array?
    By DudeJericho in forum Collections and Generics
    Replies: 2
    Last Post: April 21st, 2011, 12:39 PM
  5. Converting a method from ArrayList so it is capable with an Array
    By BlueJ1 in forum Collections and Generics
    Replies: 2
    Last Post: July 8th, 2009, 05:22 PM