Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

file reading problem  RSS feed

 
Rakesh Rt singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please guide.its urgent.
I want to read two text files.file A has data like
{1 51} -> {101} 5.65
{51 71} -> {101} 8.65
and file B has data like
{51} -> {101} 45
{1} -> {101} 63
{72} -> {101} 56
{4} -> {101} 100
{8} -> {101} 100
{1 51} -> {101} 100
for each entry of file A (eg. {1 51}->{101} 5.65, I want to search for {1} ->{101} and {51}->{101} in file B and store the corresponding values of last column(45 and 63 here) in two variables.)
 
Devaka Cooray
Marshal
Posts: 5323
575
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please EaseUp. This task can be done with the following steps

1. Read both files and process each line.
2. Identify the tokens of each line.
3. Map the identified tokens in file A with the tokens in file B.
4. Produce the desired output.

Let us know what you have already done, so we can help you more.
 
Rakesh Rt singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi , i was trying ti store all columns in separate array in following code but its not working. Even I tried with arraylists,but we cant access the list via indexes there.

try {

int i;

int []ar1 =new int[50];
int []ar2 =new int[50];
int []ar3 =new int[50];
double []ar4 =new double[50];

Scanner myfile = new Scanner(new FileReader("testinput11.txt"));

for(i=0;i<=50;i++)
{
ar1[i]=myfile.nextInt();
ar2[i]=myfile.nextInt();
ar3[i]=myfile.nextInt();


ar4[i]=myfile.nextFloat();
}


myfile.close();
for(i=0;i<=50;i++)
{
System.out.println("column 1elements are:\n" + ar1[i]);
System.out.println("column 2 elements are:\n" + ar2[i]);
System.out.println("column 3 elements are:\n" + ar3[i]);
System.out.println("column 4 elements are:\n" + ar4[i]);
}}

catch (Exception e)
{ e.printStackTrace();
}
 
Devaka Cooray
Marshal
Posts: 5323
575
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh Rt singh wrote:but we cant access the list via indexes there.


I'm not sure what you meant, but the get() method of a List implementer can be used to get the elements on indexes, given that the elements in the list follow the insertion order. There are different ways to accomplish your task. Though it's not impossible to get it done by reading through the 'columns', I'd read the file line-by-line, parse each line, inject the parsed values into objects of a class, and place those objects into an ArrayList. Or, if each line doesn't represent an entity, you can assign the parsed tokens of each line into a double array, and place each of these arrays into an ArrayList. This is ideally what you need in that case:

ArrayList<double[]> list = new ArrayList<double[]>();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!