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: Reading from a text file and storing in arrayList

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading from a text file and storing in arrayList

    Hi everybody

    I have wrriten a code to save data from a text file into any array list. I am using array list for the first time. I have used only array before. In the text file I have data that is seperated by comma , I need each element to be stored in a seperate array position that is .....

    1,10,0,0,3,2

    in the above line number one should be stored in array position 1,1......number 10 should be stored in array position 1,2...like a two dimensional array.....here is my code...

    package heuristic;
    import java.io.*;
    import java.util.Scanner;
    import java.text.*;
    import java.util.ArrayList;
    /**
     *
     * @author bharathwaj
     */
    public class greedyAlgorithm {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException
        {
            // Declarations
          ArrayList<String> patientDetails = new ArrayList<String>();
     
           int number;
     
          readPatientDetails(patientDetails,"patientdetails.txt");
     
          printArray(patientDetails,9);
     
        }
     
        public static void readPatientDetails(ArrayList<String>patientDetails, String fileName)throws IOException
        {
     
            File inFile = new File(fileName);
            String line;
     
            if (!inFile.exists())
            {
                System.out.println("Cannot find " + fileName);
                System.exit(0);
            }
            Scanner myFile = new Scanner(inFile);
           int count = 0;
     
            for(int i = 0; myFile.hasNext(); i = i+1)
            {
     
                {
                patientDetails.add(myFile.nextLine());
     
                count ++;
                }
     
            }
            myFile.close();
     
        }
     
     public static void printArray(ArrayList<String>patientDetails, int numValues)
        {
     
            for (int i = 0; i < numValues; i++)
            {
                System.out.println(patientDetails);
     
            }   
     
     
    }
    }
    Attached Files Attached Files


  2. #2
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: Reading from a text file and storing in arrayList

    Quote Originally Posted by nynamyna View Post
    Hi everybody

    I have wrriten a code to save data from a text file into any array list. I am using array list for the first time. I have used only array before. In the text file I have data that is seperated by comma , I need each element to be stored in a seperate array position that is .....

    1,10,0,0,3,2

    in the above line number one should be stored in array position 1,1......number 10 should be stored in array position 1,2...like a two dimensional array.....here is my code...

    package heuristic;
    import java.io.*;
    import java.util.Scanner;
    import java.text.*;
    import java.util.ArrayList;
    /**
     *
     * @author bharathwaj
     */
    public class greedyAlgorithm {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException
        {
            // Declarations
          ArrayList<String> patientDetails = new ArrayList<String>();
     
           int number;
     
          readPatientDetails(patientDetails,"patientdetails.txt");
     
          printArray(patientDetails,9);
     
        }
     
        public static void readPatientDetails(ArrayList<String>patientDetails, String fileName)throws IOException
        {
     
            File inFile = new File(fileName);
            String line;
     
            if (!inFile.exists())
            {
                System.out.println("Cannot find " + fileName);
                System.exit(0);
            }
            Scanner myFile = new Scanner(inFile);
           int count = 0;
     
            for(int i = 0; myFile.hasNext(); i = i+1)
            {
     
                {
                patientDetails.add(myFile.nextLine());
     
                count ++;
                }
     
            }
            myFile.close();
     
        }
     
     public static void printArray(ArrayList<String>patientDetails, int numValues)
        {
     
            for (int i = 0; i < numValues; i++)
            {
                System.out.println(patientDetails);
     
            }   
     
     
    }
    }
    hi sorry, i didnt get your point.
    [ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]
    [ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]
    [ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]
    [ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]
    [ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]
    [ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]
    [ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]
    [ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]
    [ 1,10,0,0,3,2, 2,14,0,0,3,3, 3,9,0,0,2,1, 4,15,0,0,2,1, 5,11,0,0,2,3, 6,12,0,0,3,5, 7,15,0,0,3,5, 8,11,0,0,2,2, 9,15,0,0,3,4, 10,14,0,0,2,4]

    is that you want to achieve?

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a text file and storing in arrayList

    Hi Truffy

    Thank you for the help......I already finished doing that......Thank you very much

    Bharath

Similar Threads

  1. 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
  2. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 09:10 AM
  3. Reading .txt and storing into an array
    By vluong in forum Collections and Generics
    Replies: 1
    Last Post: January 4th, 2010, 02:07 PM
  4. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 06:16 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM