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: Create Multiple Arrays from a text file

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

    Default Create Multiple Arrays from a text file

    Hi,

    I am having trouble creating multiple arrays from each line in a text file. This is my text file:

    2 8 8
    3 6 9
    3 1 7

    I am able to read this file in, create tokens and print all the values. My goal now is to create 3 arrays from this file. Array1 = [2, 8, 8], Array2 = [3, 6, 9], and Array3 = [3, 1, 7]

    This is my code so far. Please let me know if you can help or direct me to a good link online.

    Thanks, Chris


    import java.io.*;
    import java.util.StringTokenizer;

    public class Project1 {

    public static void createArrays(){
    int[] nums = null;
    try {
    File file1 = new File("file1.txt");
    FileReader fileReader = new FileReader(file1);
    BufferedReader reader = new BufferedReader(fileReader);
    String line = null;
    while ((line = reader.readLine()) != null){
    System.out.println(line);
    StringTokenizer st = new StringTokenizer (line);
    while (st.hasMoreTokens()){
    String token = st.nextToken();
    }

    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }

    }

    public static void main(String [] args){
    createArrays();
    }
    }


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Thumbs up Re: Create Multiple Arrays from a text file

    import java.io.*;
    import java.util.StringTokenizer;
     
    public class Project1 {
     
    public static void createArrays(){
    int[] nums = null;
     
    try {
    File file1 = new File("file1.txt");
    FileReader fileReader = new FileReader(file1);
    BufferedReader reader = new BufferedReader(fileReader);
    String line = null;
     
    while ((line = reader.readLine()) != null){
     
    	StringTokenizer st = new StringTokenizer (line);
     
    	nums = new int[st.countTokens()];
     
    	//System.out.println(line);
     
    	int i=0;
    		while (st.hasMoreTokens()){
    			String token = st.nextToken();
    			nums[i] = Integer.parseInt(token);
    			i++;
    		}
    		System.out.print("[");
    		for(int j=0;j<i;j++){
    			System.out.print(" "+nums[j]+" ");
    		}
    		System.out.println("]");
     
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
     
    }
     
    public static void main(String [] args){
    createArrays();
    }
    }

    i feel this code will help you to move further.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. The Following User Says Thank You to DanBrown For This Useful Post:

    chris11kgf (February 11th, 2011)

Similar Threads

  1. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  2. Replies: 0
    Last Post: December 1st, 2010, 06:10 AM
  3. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  4. create a file
    By mos33 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 4th, 2009, 02:21 PM
  5. How to create exe file
    By sirimalla in forum Java Theory & Questions
    Replies: 6
    Last Post: November 1st, 2009, 04:07 AM