i am working on a project that recieves a url from the user and then outputs the number count of each character. the project i am working on now follows on with that concept exept it is supposed to read in multiple urls and analyze each one, and then output the total character count at the end.
i have the basic program here, that just reads in one url
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.*;
public class LetterFrequency
{
public static void main(String[] args) throws IOException
{
final String page;
if(args.length == 0)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter URL address in 'http://' format: ");
page = scan.next();
}
else
{
page = args[0];
}
LetterFrequency lf = new LetterFrequency(page);
lf.run();
}
protected final PageGetter thePageGetter;
protected final Map<Character, Integer> theCounts;
public LetterFrequency(final String page) throws IOException
{
thePageGetter = new PageGetter(page);
theCounts = new TreeMap<Character, Integer>();
}
public void run() throws IOException {
thePageGetter.run();
String contents = thePageGetter.getContents();
final int length = contents.length();
for (int i = 0; i < length; ++i) {
char ch = contents.charAt(i);
if (Character.isLetterOrDigit(ch))
process(ch);
}
}
protected void process(final char ch) {
Integer count = theCounts.get(ch);
if (count == null)
theCounts.put(ch, 1);
else
theCounts.put(ch, count+1);
}
protected void displayResults() {
Set<Character> keys = theCounts.keySet();
for (Character ch: keys)
System.out.println(ch + ": " + theCounts.get(ch));
}
}
sample output
Enter URL address in 'http://' format: http://www.hotmail.com 0: 92 1: 112 2: 90 3: 62 4: 24 5: 54 6: 25 7: 12 8: 32 9: 5 A: 7 B: 24 C: 8 D: 19 E: 4 F: 37 G: 5 H: 6 I: 25 J: 7 K: 6 L: 21 M: 14 N: 5 O: 4 P: 12 Q: 7 R: 9 S: 21 T: 13 U: 8 V: 5 W: 9 X: 2 Y: 4 Z: 2 a: 118 b: 24 c: 72 d: 49 e: 163 f: 59 g: 43 h: 44 i: 145 j: 7 k: 14 l: 105 m: 61 n: 109 o: 100 p: 104 q: 10 r: 173 s: 152 t: 164 u: 44 v: 84 w: 62 x: 21 y: 29 z: 1
i am using that program in a new one that extends it, it is supposed to read in multiple urls from the user, analyuze them one by one, storing the data in a map, and then output the total character count.
i am having some issues with it as it will not compile, and i am not sure why.
here is is:
import java.util.*;
public class Urlscan extends LetterFrequency
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList<String> websta = new ArrayList<String>();
LetterFrequency l = new LetterFrequency();
}
}
please tell me why Urlscan is not compiling, i can't see why?
also, i'd appreciate it if anyone could provide me the best way to request multiple urls from the user (i'm planning a for loop, maybe a while) and then store each analysis in a map, and then combining all maps(with data) into a single output map with character totals
thanks
**********************************************************************************
EDIT after reading over my assignment requirements, i need to make the program read in the urls from a .txt file, where each url is separated by whitespace. how do i read input from a .txt file?
how would i implement it with this program, same as above but does not ask for input from user:
public class LetterFrequency {
public static void main(String[] args) throws IOException
{
LetterFrequency lf = new LetterFrequency(args[0]);
lf.run();
}
protected final PageGetter thePageGetter;
protected final Map<Character, Integer> theCounts;
public LetterFrequency(final String page) throws IOException
{
thePageGetter = new PageGetter(page);
theCounts = new TreeMap<Character, Integer>();
}
public void run() throws IOException
{
thePageGetter.run();
String contents = thePageGetter.getContents();
final int length = contents.length();
for (int i = 0; i < length; ++i)
{
char ch = contents.charAt(i);
if (Character.isLetterOrDigit(ch))
process(ch);
}
displayResults();
}
protected void process(final char ch)
{
Integer count = theCounts.get(ch);
if (count == null)
theCounts.put(ch, 1);
else
theCounts.put(ch, count+1);
}
protected void displayResults()
{
Set<Character> keys = theCounts.keySet();
for (Character ch: keys)
System.out.println(ch + ": " + theCounts.get(ch));
}
}
EDIT**************************
well i made a start on creating the file to read in the urls from a .txt file, now this new program compiles if i comment out 'extends LetterFrequency,' but when i run the program it seems to freeze, as if the computer is trying to find the .txt file which i have saved to the same directory.
what is going on?
how do i implement LetterFrequency (code above) functionality with this new program?
here is the .txt file: urlreadsta.txt
http://www.hotmail.com http://www.google.com http://www.dreamincode.net
and my program so far:
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.io.*;
import java.util.*;
import java.lang.*;
public class UrlRead //extends LetterFrequency //*** compiles with 'extends...' commented out
{
public static void main(String[] args)throws FileNotFoundException
{
String url;
String putout = "Here is the URL and it's data: \n";
Scanner UrlReadstaInput = null;
File inUrlReadstaFile = new File("urlreadsta.txt");
try
{
UrlReadstaInput = new Scanner (inUrlReadstaFile);
while (UrlReadstaInput.hasNext());
{
url = UrlReadstaInput.next();
putout += String.format(url);
}
System.out.println(putout);
}//end try
catch(FileNotFoundException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
finally
{
UrlReadstaInput.close();
}
}
}
any help appreciated
????????????????????????????????????????????????????????????????????????????????
EDIT EDIT EDIT
so i tried it again, i think my intial program for reading in data from a .txt was incorrect and complicated, so i tried it again.
now the problem i am having is that it won't compile saying "an if without an else" i ve been messing around with {} trying to get it to work but it doesn't seem to help.
i also put in the LetterFrequency program so that, urlread will read in urls from urlreadsta.txt and then use letterfrequency to analyze them each and print the output.
what did i do wrong, how can i get it to compile? it won't compile also is 'extends...' is not commented out
thanks again
latest iteration
import java.util.*;
import java.io.*;
public class UrlRead //extends LetterFrequency
{
public static void main (String[]args)throws FileNotFoundException
{
Scanner in = new Scanner(new File("urlreadsta.txt"));
while (in.hasNext())
{
String i = in.next();
if (in.next() == args.length == 0);
{
i = in.nextLine();
}
else
{
i = args[0];
}
LetterFrequency lf = new LetterFrequency(i);
lf.run();
}
}
protected final PageGetter thePageGetter;
protected final Map<Character, Integer> theCounts;
public void LetterFrequency(final String page) throws IOException {
thePageGetter = new PageGetter(page);
theCounts = new TreeMap<Character, Integer>();
}
public void run() throws IOException {
thePageGetter.run();
String contents = thePageGetter.getContents();
final int length = contents.length();
for (int i = 0; i < length; ++i) {
char ch = contents.charAt(i);
if (Character.isLetterOrDigit(ch))
process(ch);
}
displayResults();
}
protected void process(final char ch) {
Integer count = theCounts.get(ch);
if (count == null)
theCounts.put(ch, 1);
else
theCounts.put(ch, count+1);
}
protected void displayResults() {
Set<Character> keys = theCounts.keySet();
for (Character ch: keys)
System.out.println(ch + ": " + theCounts.get(ch));
}
}
__________________________________________________________
EDIT
____________________________________________________________
latest iteration, tried eliminating the if else loop but still won't compile.
help appreciated.
latest iteration:
import java.util.*;
import java.io.*;
public class UrlRead extends LetterFrequency
{
public static void main (String[]args)throws FileNotFoundException
{
Scanner in = new Scanner(new File("urlreadsta.txt"));
final String page;
if (args.length == 0)
{
page = in.next();
}
else
{
page = args[0];
}
LetterFrequency lf = new LetterFrequency(page);
lf.run();
}
protected final PageGetter thePageGetter;
protected final Map<Character, Integer> theCounts;
public void LetterFrequency(final String page) throws IOException {
thePageGetter = new PageGetter(page);
theCounts = new TreeMap<Character, Integer>();
}
public void run() throws IOException {
thePageGetter.run();
String contents = thePageGetter.getContents();
final int length = contents.length();
for (int i = 0; i < length; ++i) {
char ch = contents.charAt(i);
if (Character.isLetterOrDigit(ch))
process(ch);
}
displayResults();
}
protected void process(final char ch) {
Integer count = theCounts.get(ch);
if (count == null)
theCounts.put(ch, 1);
else
theCounts.put(ch, count+1);
}
protected void displayResults() {
Set<Character> keys = theCounts.keySet();
for (Character ch: keys)
System.out.println(ch + ": " + theCounts.get(ch));
}
}
compilation errors - using jgrasp
UrlRead.java:4: cannot find symbol symbol : constructor LetterFrequency() location: class LetterFrequency public class UrlRead extends LetterFrequency ^ UrlRead.java:28: cannot assign a value to final variable thePageGetter thePageGetter = new PageGetter(page); ^ UrlRead.java:29: cannot assign a value to final variable theCounts theCounts = new TreeMap<Character, Integer>(); ^ 3 errors
This post has been edited by scholio: 08 May 2008 - 08:50 PM

New Topic/Question
Reply




MultiQuote





|