Hi all;
I'm new to java.
I have a program that is intented to get a text file (in here,ScanTest.txt) containing some names and values such as:
name=jeremy
#This is a comment
account=1234
balance=500
....
The line starting with "#" is considered as a comment.
Then, I have a method called "getString" so that if I give a name to it, it should return corresponding value as following:
name---->jeremy
balance---->500
To implement this program, I've used "hashtable" class. Here is my code:
package io;
import java.io.*;
import java.util.*;
public class HashTableDemo {
static String fileName;
static Hashtable hash;
static Enumeration names;
public HashTableDemo() {
fileName = "D:\\Documents and Settings\\Bahareh\\Desktop\\ScanText.txt";
hash = new Hashtable();
}
public static String getString(String st)
{
return (String)hash.get(st);
}
public static void main(String args[]) throws Exception{
// Create a hash map
//Hashtable hash = new Hashtable();
try{
String st;
InputStream in = new FileInputStream(fileName);
Scanner sc = new Scanner(in).useDelimiter("=");
while (sc.hasNext()) {
hash.put(sc.next(), sc.next());
}
// Show all hashes in hash table.
names = hash.keys();
while (names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": "+ /*hash.get(str)*/getString(str));
}
System.out.println();
}
catch(IOException e)
{
System.out.print(e.getMessage());
}
}
}
When I run it, I get the error:
------------------------------------------------------
Exception in thread "main" java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.jav a:134)
at java.io.FileInputStream.<init>(FileInputStream.jav a:97)
at io.HashTableDemo.main(HashTableDemo.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
------------------------------------------------------
What's the problem?
Besides, I want to ignore the lines starting with "#" sign and not to add them to the hash table.
How should I seperate these lines?
How does java recognize that first character of a line is "#" sign?
Please help...
TIA