Welcome to Dream.In.Code
Become a Java Expert!

Join 150,181 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,142 people online right now. Registration is fast and FREE... Join Now!




Need Java code for following algorithm

 
Reply to this topicStart new topic

Need Java code for following algorithm

stjais
27 Aug, 2008 - 09:58 PM
Post #1

New D.I.C Head
*

Joined: 27 Aug, 2008
Posts: 2

* I Need Java code for following algorithm. Kindly any one help.

1. Read the contents (ideas and its corresponding scores) from two files named as 'a' and 'b'.

2. Stored the file 'a' contents in array a[].

3. Stored the file 'b' contents in array b[].

4. compare both files like

if(a[i].equals(b[j])
{
...........
Writing the common idea and add the score from file 'a' and 'b'.
..........

}
else
{
write the uncommon idea and its score..

}

For example :

--------------------------------------------------------------------------------
Form Agents.txt

--------------------------------------------------------------------------------

action,65
architecture,85
eco-,15
essay,30
form,85
form,85
link,40
tangent,25

Form Agents1.txt

--------------------------------------------------------------------------------

Black holes,69
essay,78

--------------------------------------------------------------------------------
Herewith i have above mentioned two files named as Form Agents and Form Agents1.

Form Agents has eight fields

Form Agents1 has two fields

--> 'essay' is common in two files, so store the idea 'essay' and add the score from Form Agents score is '30' and Form Agents1 has 78 (essay 108).

Finally it stores idea in another file with uncommon fields also.

Please help us.

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Need Java Code For Following Algorithm
27 Aug, 2008 - 10:05 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
It is against site policy to help without first seeing a "best effort" attempt by you to solve the problem yourself. What this means is that we would like to see what code you have written first in an attempt to solve the problem and then we can help guide you through it as to where you are getting hung up at. It also means we do not do peoples homework for them.

So if you can show us what you have so far, then we can certainly jump in and help you out where we can. Thanks for helping us help you! smile.gif
User is offlineProfile CardPM
+Quote Post

stjais
RE: Need Java Code For Following Algorithm
27 Aug, 2008 - 10:27 PM
Post #3

New D.I.C Head
*

Joined: 27 Aug, 2008
Posts: 2

CODE

try
{
DataOutputStream o1=new DataOutputStream(new

FileOutputStream("C:\\Interfaces\\interfaces\\temp\\BlackBoard\\My Design

World\\Project\\Material\\art\\System Agents\\Form Agents\\CandidateResponses\\Form

Agents.txt"));
//Reading the contents of the files
BufferedReader br= new BufferedReader(new InputStreamReader(new

FileInputStream("C:\\Interfaces\\interfaces\\temp\\BlackBoard\\My Design

World\\Project\\Material\\art\\System Agents\\Form Agents\\Ideological\\Form

Agents.txt")));
BufferedReader br1= new BufferedReader(new InputStreamReader(new

FileInputStream("C:\\Interfaces\\interfaces\\temp\\BlackBoard\\My Design

World\\Project\\Material\\art\\System Agents\\Form Agents\\Related\\Form

Agents.txt")));

while((s=br.readLine())!=null)
s1+=s+"\n";

while((s2=br1.readLine())!=null)

s3+=s2+"\n";


int numTokens = 0;
StringTokenizer st = new StringTokenizer(s1);
String[] a = new String[10000];
String[] br_n=new String[10000];
int i=0;
while (st.hasMoreTokens())
{
s2 = st.nextToken();
a[i]=s2.substring(0,s2.length()-3);
s6=s2.substring(s2.length()-2);
br_n[i]=s6;
i++;
numTokens++;
}
int numTokens1 = 0;
StringTokenizer st1 = new StringTokenizer (s3);
String[] b = new String[10000];
String[] br1_n=new String[1000];
int j=0;
while (st1.hasMoreTokens())
{
s4 = st1.nextToken();
b[j]=s4.substring(0,s4.length()-3);
s7=s4.substring(s4.length()-2);
br1_n[j]=s7;
j++;
numTokens1++;
}
int x=0;
for(int m=0;m<a.length;m++)
{

for(int n=0;n<b.length;n++)
{

if(a[m].equalsIgnoreCase(b[n])){
int sc=Integer.parseInt(br_n[m]);
int sc1=Integer.parseInt(br1_n[n]);
int score=sc+sc1;
o.writeBytes(a[m]+","score"\n");

break;

}
else
{
o.writeBytes(a[m]+","br_n[m]"\n");
break;

}

}


}

}
}
}catch(Exception e){}







Please correct the code.

For example here i showed two files named as

Form Agents.txt

action,65
architecture,85
eco-,15
essay,30
form,85
form,85
link,40
tangent,25

************************************************************
Form Agents1.txt

Black holes,69
essay,78
*************************************************************
My requirement is

Combine both files and store the following output in another file,

********************************************************************************
*

Expected Output:

action,65
architecture,85
Black holes,69
eco-,15
essay,108 (here the common field value is added)
form,85
form,85
link,40
tangent,25


********************************************************************************
****




User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:48AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month