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...
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);
}
}
}
Re: how to read content of a file and store in seperate arrays in java
Better use a small class:
Code:
class Record {
int rollNo;
String name;
int item1, item2;
}
... and store objects of that class in a single array.
kind regards,
Jos
Re: how to read content of a file and store in seperate arrays in java
i'm not gettin it...can u explain it...
Re: how to read content of a file and store in seperate arrays in java
Your way ends up with a bunch of arrays:
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):
See my previous reply for a possible definition of the Record class.
kind regards,
Jos
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.
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..
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
Re: how to read content of a file and store in seperate arrays in java
Quote:
/**
* @(#)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....
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