Results 1 to 9 of 9
- 03-24-2012, 07:39 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
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); } } }
- 03-24-2012, 08:09 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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; }
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-24-2012, 08:37 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Re: how to read content of a file and store in seperate arrays in java
i'm not gettin it...can u explain it...
- 03-24-2012, 08:51 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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;
Java Code:Records[] records;
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-24-2012, 08:57 AM #5
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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.Website: Learn Java by Examples
- 03-24-2012, 09:03 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
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..
- 03-24-2012, 09:05 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
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
- 03-24-2012, 09:19 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
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]);
}
}
[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....
- 03-24-2012, 09:25 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Read in File and Store it in an Array. Please Help!
By PurpleDemon666 in forum New To JavaReplies: 1Last Post: 10-21-2011, 08:50 PM -
How to read content from a file
By eden83 in forum New To JavaReplies: 2Last Post: 01-12-2010, 10:25 PM -
how to read content of .xls file
By kirtichopra2003 in forum Advanced JavaReplies: 10Last Post: 09-11-2009, 01:03 PM -
How do you read from a file, and then store the info in an array?
By szimme101 in forum New To JavaReplies: 5Last Post: 07-30-2008, 09:30 AM -
How to store 5 numbers from a file into 5 seperate variables?
By kewlgeye in forum New To JavaReplies: 4Last Post: 06-09-2008, 04:44 PM
Bookmarks