J
Jay Cee
Hi All,
Relatively new to java (ex VB) and could do with some help.
I need to read a text file character by character (can do),
and count each character as it appears, i.e
"A small sample text file" would have 1-A , 2-s, 2-m ,etc etc. and output
the results.
I have a few issues which I cannot seem to solve easily,
1/
I thought it would be a good idea to save the characters in a hashmap in
name-value pairs as they are read , map.put(tempStr,"1" )
I found I had to convert the character to a string before it would save to
the map.Ideally I would like to save as a character.
2/
Before adding each character to the map
check first if it already exists
and if found increment the value portion of the name value pair
else
if not found insert into map with value of 1.
My problems seems to be I cannot "check the map" if the character exists and
if it does exist how do I get at the value to increment it.
Here is what I have so far,
import java.io.*;
import java.util.*;
class TextTest
{
public static Map map = new HashMap();
private static TreeMap treeMap;
public static void main(String[] args) throws IOException
{
FileInputStream in = new FileInputStream("textfile.txt");
int ch;
int total = 0;
int count = 1;
while ((ch = in.read()) != -1)
{
total ++;
String tempStr = (Integer.toString(ch)); //Only way to save the
"char" in the map was to convert it to a string.
System.out.print((char)ch );
if (map.containsKey(tempStr))
{
map.put(tempStr,"value" ); //How can i extract
the value,increment it and save back to the map
}
else
{
map.put(tempStr, "value"); //I need to save the
integer 1 here in the value part of the map
}
}
treeMap = new TreeMap(map); //sort the map
System.out.println("Total =" + total);
System.out.print(treeMap);
}
}
Relatively new to java (ex VB) and could do with some help.
I need to read a text file character by character (can do),
and count each character as it appears, i.e
"A small sample text file" would have 1-A , 2-s, 2-m ,etc etc. and output
the results.
I have a few issues which I cannot seem to solve easily,
1/
I thought it would be a good idea to save the characters in a hashmap in
name-value pairs as they are read , map.put(tempStr,"1" )
I found I had to convert the character to a string before it would save to
the map.Ideally I would like to save as a character.
2/
Before adding each character to the map
check first if it already exists
and if found increment the value portion of the name value pair
else
if not found insert into map with value of 1.
My problems seems to be I cannot "check the map" if the character exists and
if it does exist how do I get at the value to increment it.
Here is what I have so far,
import java.io.*;
import java.util.*;
class TextTest
{
public static Map map = new HashMap();
private static TreeMap treeMap;
public static void main(String[] args) throws IOException
{
FileInputStream in = new FileInputStream("textfile.txt");
int ch;
int total = 0;
int count = 1;
while ((ch = in.read()) != -1)
{
total ++;
String tempStr = (Integer.toString(ch)); //Only way to save the
"char" in the map was to convert it to a string.
System.out.print((char)ch );
if (map.containsKey(tempStr))
{
map.put(tempStr,"value" ); //How can i extract
the value,increment it and save back to the map
}
else
{
map.put(tempStr, "value"); //I need to save the
integer 1 here in the value part of the map
}
}
treeMap = new TreeMap(map); //sort the map
System.out.println("Total =" + total);
System.out.print(treeMap);
}
}