Results 1 to 9 of 9
  1. #1
    jellora is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default how to read content of a file and store in seperate arrays in java

    i want to noe how to store the data in seperate arrays while using scanner to read the file..
    eg: skip.txt contains 1001 jessi 91 92
    1002 ellora 97 97
    1003 josh 87 91
    i want to store all the roll no in an array and names in another array, mark1 in another array,mark2 in another array...

    how to do it...
    Java Code:
    /**
     * @(#)readstring.java
     *
     *
     * @author 
     * @version 1.00 2012/2/21
     */
    
    import java.io.File;
    import java.util.*;
    public class readstring {
    
        public static void main(String args[]) throws Exception
        {
        	File f=new File("f:/java_src/data/skip.txt");
        	Scanner in=new Scanner(f);
        	String s[]=new String[3];
        	String b=new String();
        	String c=new String();
        	int i=0;
        	while(in.hasNext())
        	{
        		b=in.next();
        		
        /*	Scanner sc=new Scanner(b);
        	sc.useDelimiter("	");
        	while(sc.hasNext())
        	{
        		c=sc.nextLine();
        		System.out.println(c);
        	}*/
        	
        	System.out.println(b);
        }
        
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to read content of a file and store in seperate arrays in java

    Better use a small class:

    Java Code:
    class Record {
       int rollNo;
       String name;
       int item1, item2;
    }
    ... and store objects of that class in a single array.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jellora is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: how to read content of a file and store in seperate arrays in java

    i'm not gettin it...can u explain it...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to read content of a file and store in seperate arrays in java

    Your way ends up with a bunch of arrays:

    Java Code:
    int[] rollNo;
    String[] name;
    int[] item1;
    int[] item2;
    By using my suggestion, you'll end up with a single array (also containing all the information):

    Java Code:
    Records[] records;
    See my previous reply for a possible definition of the Record class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: how to read content of a file and store in seperate arrays in java

    Instead of using many array to store information of your data you can store each line of your data into a single object. You can create a Record class and store each line your read from the file into the fields in that class. This will make your data structure simpler.

    And Jos have given you an example of that class that you can use to store the student information.

  6. #6
    jellora is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: how to read content of a file and store in seperate arrays in java

    okeee..another ques.. i store it al in one array, if i'm searchin the string ellora or 1002..it has to return the whole line...

    i'm thinkin of using contains method..but how to retrieve a line from middle of the array..

  7. #7
    jellora is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: how to read content of a file and store in seperate arrays in java

    ur right..i noe how to store in one array..but i wanna noe how to store in different arrays without using any api's..i stored using arraylist..but i want to store without any api's

  8. #8
    jellora is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: how to read content of a file and store in seperate arrays in java

    /**
    * @(#)readstring.java
    *
    *
    * @author
    * @version 1.00 2012/2/21
    */

    import java.io.*;
    import java.util.*;
    import java.lang.*;
    public class extractwholestring {

    public static void main(String args[]) throws Exception
    {


    List<String> names = new ArrayList<String>();


    Scanner myfile = new Scanner(new FileReader("f:/java_src/data/skip.txt"));

    while (myfile.hasNext()) {

    names.add(myfile.nextLine());

    }

    System.out.println("NAMES:\n" + names);

    boolean p= names.contains("janet");
    String a[]=names.toArray(new String[names.size()]);
    System.out.println(a[0]);

    }
    }
    desired output:NAMES:
    [1001 janet cse 91 92, 1002 joshua eee 94 89, 1003 poline it 98 90, 1004 john ece 89 99, 1005 thersa msc 98 95]
    1001 janet cse 91 92

    here i am externally printing the line in which the name is present..but i want it print if the name is found....

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to read content of a file and store in seperate arrays in java

    Where is that Record class I suggested?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Read in File and Store it in an Array. Please Help!
    By PurpleDemon666 in forum New To Java
    Replies: 1
    Last Post: 10-21-2011, 08:50 PM
  2. How to read content from a file
    By eden83 in forum New To Java
    Replies: 2
    Last Post: 01-12-2010, 10:25 PM
  3. how to read content of .xls file
    By kirtichopra2003 in forum Advanced Java
    Replies: 10
    Last Post: 09-11-2009, 01:03 PM
  4. Replies: 5
    Last Post: 07-30-2008, 09:30 AM
  5. Replies: 4
    Last Post: 06-09-2008, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •