R
R.Venkat
i have 2 data files each with a string and an integer separated by a
space/tab. i have to compare the integer values based on the string as
the key.i wrote the following piece of code:
-------------------------------Start Code---------------------------
import java.util.*;
import java.io.*;
class DataIntegrityTest
{
public static void main(String[] args) throws Exception
{
Hashtable lengths1 = new Hashtable<String,Integer>();
Hashtable lengths2 = new Hashtable<String,Integer>();
BufferedReader in=new BufferedReader(new
FileReader("d:\\data1.txt"));
String s="";
while((s=in.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(s," ");
String columnName=st.nextToken();
int length = Integer.parseInt(st.nextToken());
lengths1.put(columnName,length);
}
in.close();
BufferedReader in1=new BufferedReader(new
FileReader("d:\\data2.txt"));
String s1="";
while((s1=in1.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(s1," ");
String columnName=st.nextToken();
int length = Integer.parseInt(st.nextToken());
lengths2.put(columnName,length);
}
in1.close();
for(Enumeration e = lengths1.keys();e.hasMoreElements()
{
String key = (String) e.nextElement();
if(((Integer)lengths1.get(key)).intValue() <
((Integer)lengths2.get(key)).intValue())
{
System.out.println(key+" "+ lengths1.get(key) +" "+
lengths2.get(key));
}
}
}
}
-------------------------------End Code---------------------------
the for loop that i've used does not fully use the new JDK 1.5 foreach
loop. Any tips as how to enhance this piece of code so that it uses
the 'foreach' loop in JDK1.5?
Thanks
space/tab. i have to compare the integer values based on the string as
the key.i wrote the following piece of code:
-------------------------------Start Code---------------------------
import java.util.*;
import java.io.*;
class DataIntegrityTest
{
public static void main(String[] args) throws Exception
{
Hashtable lengths1 = new Hashtable<String,Integer>();
Hashtable lengths2 = new Hashtable<String,Integer>();
BufferedReader in=new BufferedReader(new
FileReader("d:\\data1.txt"));
String s="";
while((s=in.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(s," ");
String columnName=st.nextToken();
int length = Integer.parseInt(st.nextToken());
lengths1.put(columnName,length);
}
in.close();
BufferedReader in1=new BufferedReader(new
FileReader("d:\\data2.txt"));
String s1="";
while((s1=in1.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(s1," ");
String columnName=st.nextToken();
int length = Integer.parseInt(st.nextToken());
lengths2.put(columnName,length);
}
in1.close();
for(Enumeration e = lengths1.keys();e.hasMoreElements()
{
String key = (String) e.nextElement();
if(((Integer)lengths1.get(key)).intValue() <
((Integer)lengths2.get(key)).intValue())
{
System.out.println(key+" "+ lengths1.get(key) +" "+
lengths2.get(key));
}
}
}
}
-------------------------------End Code---------------------------
the for loop that i've used does not fully use the new JDK 1.5 foreach
loop. Any tips as how to enhance this piece of code so that it uses
the 'foreach' loop in JDK1.5?
Thanks