Reputation: 826
I have a text file, I want to read it and place it into my hash table. Then print it.
I have written a block of code, what am I doing wrong?
public static void main(String[] args) throws FileNotFoundException, IOException {
Hashtable< Integer, String > hash = new Hashtable< Integer, String >();
BufferedReader rd = new BufferedReader( new FileReader ("students.txt"));
String line = "";
int i = 0;
while (line != null){
line = rd.readLine();
hash.put(i, line);
i++;
}
for ( int j = 0 ; j < hash.size() ; j++){
System.out.println(hash.get(j));
}
}
Upvotes: 0
Views: 8970
Reputation: 11
I am using your code and I have corrected some bugs...
I think, this code is not right but he works :)
try{
Hashtable< Integer, String > hash = new Hashtable< Integer, String >();
BufferedReader rd = new BufferedReader( new FileReader ("students.txt"));
String line;
int i = 0;
while ((line = rd.readLine()) != null){
hash.put(i, line);
i++;
}
for ( int j = 0 ; j < hash.size() ; j++){
System.out.println(hash.get(j));
}
}catch(FileNotFoundException e){}catch (IOException e) {}
Upvotes: 1
Reputation: 34424
Code looks good . Correcting one bug below
BufferedReader br = new BufferedReader(new FileReader ("students.txt"));
while ((thisLine = br.readLine()) != null) {
System.out.println(thisLine);
}
Upvotes: 1