url character reader, compile help, reading data from .txt file

compile error - read in from .txt file, store url data in a map and th

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2484 Views - Last Post: 09 May 2008 - 05:13 PM Rate Topic: -----

#1 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

url character reader, compile help, reading data from .txt file

Post icon  Posted 08 May 2008 - 02:51 PM

hello all

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


Is This A Good Question/Topic? 0
  • +

Replies To: url character reader, compile help, reading data from .txt file

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: url character reader, compile help, reading data from .txt file

Posted 08 May 2008 - 08:59 PM

Can't beleive so much code for such a task
This should do it in a little bit more than 20 lines

import java.util.Scanner;

public class LetterFrequency 
{  
  public static void main(String[] args) {
	  int[] count = new int[255];
	  Scanner scan = new Scanner(System.in);
	  while(true) {
	System.out.print("Enter URL: ");
	String str = scan.nextLine();
	if(str.length() == 0)	// exit if only <cr>
		break;
	for(int i = 0; i < str.length(); i++)
	   count[str.charAt(i)]++;
	  }
	  int total = 0;
	  for(int i = 0; i < count.length; i++) {
		 if(count[i] != 0) {
			total += count[i];
			char digit = (char) i;
			System.out.println("Nb: " + digit + " " + count[i]);
	   } 
	   System.out.println("Total characters read: ", total);
 }



Or I really miss something
KISS: keep it stupid simple

This post has been edited by pbl: 08 May 2008 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: url character reader, compile help, reading data from .txt file

Posted 08 May 2008 - 09:06 PM

View Postpbl, on 8 May, 2008 - 08:59 PM, said:

Can't beleive so much code for such a task
This should do it in a little bit than 20 lines

import java.util.Scanner;

public class LetterFrequency 
{  
  public static void main(String[] args) {
	  int[] count = new int[255];
	  Scanner scan = new Scanner(System.in);
	  while(true) {
		  System.out.print("Enter URL: ");
		  String str = scan.nextLine();
		  if(str.length() == 0)
			  break;
		  for(int i = 0; i < str.length(); i++)
			  count[str.charAt(i)]++;
	  }
	  for(int i = 0; i < count.length; i++) {
		  if(count[i] != 0) {
			  char digit = (char) i;
			  System.out.println("Nb: " + digit + " " + count[i]);
		  }
	  }
	} 
 }



Or I really miss something


pbl, thanks, what you provided is similar to what i want except i need to read in the urls from a .txt file and then read each url one by one and then do the analysis for each (letter frequency).

output should have each urls letter frequency, and after the program has done all urls in the .txt file, it then prints out the total stats for all processed states

how do i read each url from a .txt file, i posted mine earlier in the massive post, actually below.

also, i like how formatted the output, how do i go about, printing the totals of all letters/characters of all processed urls, after printing each urls stats individually?

urlreadsta.txt

http://www.hotmail.com
http://www.google.com
http://www.ebay.com



thanks again
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: url character reader, compile help, reading data from .txt file

Posted 08 May 2008 - 09:12 PM

Just replace

Scanner scan = new Scanner(System.in);

by

Scanner scan = new Scanner("file.txt");

in your loop for each line do:
int[] urlCount = new int[255];



then
 for(int i = 0; i < str.length(); i++) {
	 count[str.charAt(i)]++;
	 urlCount[str.charAt(i))++;
   }



print, in a loop similar that the one I provided the urlCount

This post has been edited by pbl: 08 May 2008 - 09:30 PM

Was This Post Helpful? 0
  • +
  • -

#5 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 06:37 AM

pbl, thanks in which code do i substitute the above fragments? urlread? if so where exactly, also why did you choose 255? what significance does that number have?

also i think you're missing a '],' not sure where though
urlCount[str.charAt(i))++;



thanks

____________________________________
EDIt

now this is my basic basic program that successfully access the .txt file and prints out its content


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 lf = new LetterFrequency();
			String lf = in.nextLine();
			System.out.println(lf);
		}
		  //LetterFrequency lf = new LetterFrequency(page);
		  //lf.run();
		
	}
}	
	



now, i want to add code to UrlRead so that it can use LetterFrequency, to read in each url from .txt file and process them.

what do i need to do, 'ive tried if-else loops in the previous attempts but they did not work...

i need to create a new letterfrequency for each url from the.txt file, then process them one by one

here is Letter Frequency

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));
  } 

} 



this slightly modified LetterFrequency requests the url from the user via scanner


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);
	} 
	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));
  } 

} 



This post has been edited by scholio: 09 May 2008 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 06:49 AM

View Postscholio, on 9 May, 2008 - 06:37 AM, said:

pbl, thanks in which code do i substitute the above fragments? urlread? if so where exactly, also why did you choose 255? what significance does that number have?

also i think you're missing a '],' not sure where though
urlCount[str.charAt(i))++;



thanks


255 numbers of Ascii characters unless you enter the URL in simplified chineese or cyrillic

import java.util.Scanner;

public class LetterFrequency 
{  
	public static void main(String[] args) {
		int[] count = new int[255];
		Scanner scan = new Scanner("file.txt");
		while(scan.hasNextLine()) {
			String url = scan.nextLine();
			int[] urlCount = new int[255];
			for(int i = 0; i < url.length(); i++) {
				count[url.charAt(i)]++;
				urlCount[url.charAt(i)]++;
			} 
			System.out.println("Count for: " + url);
			for(int i = 0; i < urlCount.length; i++) {
				if(urlCount[i] != 0) {
					char digit = (char) i;
					System.out.println("Nb: " + digit + " " + urlCount[i]);
				}
			}
		}

		for(int i = 0; i < count.length; i++) {
			if(count[i] != 0) {
				char digit = (char) i;
				System.out.println("Nb: " + digit + " " + count[i]);
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 07:46 AM

thanks pbl, thats pretty much what i needed it to, do i made some modifications o that i could inport and read from a .txt file, i imported java.io.* and put new File before"file.txt").

i am trying us a the Thread.sleep function, i want the program to wait 3 seconds before processing the next url, i understand i need to handle an InterruptedException.

where should i put Thread.sleep(3000) -->3000=3000ms=3s and how should i go about handling the exception that will pop up?

__________EDIT______________

must of missed something when i was running the program. i believe the current program just counts the number of chracters in the .txt file correct. i should have specified this earlier, but actually the porgram reads the url from the .txt file, access that website and then counts the number of characters from the raw html - numbers and letters.

i think i need use functionality, namely making the internet connection, using LetterFrequency, how do i do so? do i use inheritance, if i do, i tried before but for some reason the "extends LetterFrequency" always triggered a compile error

thanks again

This post has been edited by scholio: 09 May 2008 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 09:01 AM

[quote name='scholio' date='9 May, 2008 - 07:46 AM' post='353378']
thanks pbl, thats pretty much what i needed it to, do i made some modifications o that i could inport and read from a .txt file, i imported java.io.* and put new File before"file.txt").

i am trying us a the Thread.sleep function, i want the program to wait 3 seconds before processing the next url, i understand i need to handle an InterruptedException.
where should i put Thread.sleep(3000) -->3000=3000ms=3s and how should i go about handling the exception that will pop up?
[quote]

try {
   Thread.sleep(3000);
}
catch(Exception e) {
}



The chances that you hit an exception are almost 0. Don't bother doing something... anyhow there is almost nothing you can do beside trying again

You should put the code just before the closing } of your while loop that reads from the file

Quote

must of missed something when i was running the program. i believe the current program just counts the number of chracters in the .txt file correct.


Yes

Quote

i should have specified this earlier, but actually the porgram reads the url from the .txt file, access that website and then counts the number of characters from the raw html - numbers and letters.


Little detail yes. That is a completly different ball game.

Quote

i think i need use functionality, namely making the internet connection, using LetterFrequency, how do i do so? do i use inheritance, if i do, i tried before but for some reason the "extends LetterFrequency" always triggered a compile error


no need to extend your actual program.
You will just have to modify it... doesn't need the count the number of letters in the .txt file but to count the number of letters in the URL pages pointed by these URL String.
Was This Post Helpful? 0
  • +
  • -

#9 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 09:23 AM

thanks again pbl, how would you suggest the best way to create a letterfrequency object for each url from the .txt file?

also, since i don't need to extend letterfrequency, what do i modify, do i need to copy over code from letterfrequency? if so which code? what modifications are you talking about?

here is what i have now, it compiles but has runtime exception

import java.util.Scanner;
import java.io.*;  
   
 public class LetterFreq
 {	
	 public static void main(String[] args)throws FileNotFoundException, IOException
	 {  
		 int[] count = new int[255];  
		 Scanner scan = new Scanner(new File("urlreadsta.txt"));  
		 while(scan.hasNextLine()) 
	 {  
			 String url = scan.nextLine();
				 url = 
(args[0]);
				 LetterFrequency fl = new LetterFrequency(url);
				 fl.run(); 
		 }  
   
	 }
}




runtime exception

 ----jGRASP exec: java LetterFreq

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at LetterFreq.main(LetterFreq.java:13)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.




do i need do a loop at line 13, where the exception occurred similar to this(see below) in order for it to run properly with no exceptions?

final String url;  
if(args.length == 0)	
	 	{  
	   	Scanner scan = new Scanner(System.in);  
		  System.out.print("Enter URL address in 'http://' format: ");  
		  url = scan.next();	
		  }  
	 	else  
		{   
	   	url = args[0];   
	   }	 
 
	 LetterFrequency lf = new LetterFrequency(url);



________________________EDIT_________________________

tried the new code with the if-else loop, it compiled but had much more, different runtime exceptions

import java.util.Scanner;
import java.io.*;  

public class LetterFreq 
{	
	public static void main(String[] args)throws FileNotFoundException, IOException
	 {  
		  
		Scanner scan = new Scanner(new File("urlreadsta.txt"));  
		while(scan.hasNextLine())
		  {  
			String url = scan.nextLine();
				if (args.length == 0)
				{					
					url = scan.next();
				}  
	 			else   
 				{   
	   			url = args[0];  
			   }	 
 
				LetterFrequency fl = new LetterFrequency(url);
				fl.run(); 
		}  
   
	}
}



runtime exceptions

 ----jGRASP exec: java LetterFreq

Exception in thread "main" java.io.FileNotFoundException: http://userpages.umbc.edu/~jmartens/courses/thisDoesNotExist.nope
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at PageGetter.run(PageGetter.java:35)
	at LetterFrequency.run(LetterFrequency.java:114)
	at LetterFreq.main(LetterFreq.java:23)
Caused by: java.io.FileNotFoundException: http://userpages.umbc.edu/~jmartens/courses/thisDoesNotExist.nope
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(Unknown Source)
	at java.net.URLConnection.getHeaderFieldInt(Unknown Source)
	at java.net.URLConnection.getContentLength(Unknown Source)
	at PageGetter.run(PageGetter.java:34)
	... 2 more

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.


This post has been edited by scholio: 09 May 2008 - 09:32 AM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 09:46 AM

View Postscholio, on 9 May, 2008 - 09:23 AM, said:

thanks again pbl, how would you suggest the best way to create a letterfrequency object for each url from the .txt file?


That is a good idea... will split the code


also, since i don't need to extend letterfrequency, what do i modify, do i need to copy over code from letterfrequency? if so which code? what modifications are you talking about?

here is what i have now, it compiles but has runtime exception

import java.util.Scanner;
import java.io.*;  
   
 public class LetterFreq
 {	
	 public static void main(String[] args)throws FileNotFoundException, IOException
	 {  
		 int[] count = new int[255];  
		 Scanner scan = new Scanner(new File("urlreadsta.txt"));  
		 while(scan.hasNextLine()) 
	 {  
			String url = scan.nextLine();
			url = (args[0]);
						LetterFrequency fl = new LetterFrequency(url);
			 fl.run(); 
		 }  
   
	 }
}



url = arg[0];

does not make sense. You have just put in the String url the next line from the file
why you do replace it by the parameter you passed to your main() method
If there a 10 lines in your file, you will just create 10 LetterFrequency and fl.run() with the arg[0] argument



// if url is final you can't modify it so don't try to scan.next() into it or to make it = to arg[0]
final String url;  
if(args.length == 0)	
	 	{  
	   	Scanner scan = new Scanner(System.in);  
		  System.out.print("Enter URL address in 'http://' format: ");  
		  url = scan.next();	
		  }  
	 	else  
		{   
	   	url = args[0];   
	   }	 



________________________EDIT_________________________

tried the new code with the if-else loop, it compiled but had much more, different runtime exceptions

import java.util.Scanner;
import java.io.*;  

public class LetterFreq 
{	
	public static void main(String[] args)throws FileNotFoundException, IOException
	 {  
		  
		Scanner scan = new Scanner(new File("urlreadsta.txt"));  
		while(scan.hasNextLine())
		  {  
			String url = scan.nextLine();
				if (args.length == 0)
				{					
					url = scan.next();
				}  
	 			else   
 				{   
	   			url = args[0];  
			   }	 
 
				LetterFrequency fl = new LetterFrequency(url);
				fl.run(); 
		}  
   
	}
}



I guess this is what you want

import java.util.Scanner;
import java.io.*;  

public class LetterFreq 
{	
	public static void main(String[] args)throws FileNotFoundException, IOException
	 {  
					   // if there are arguments
					   if(args.length != 0) {
							// use each of them has url
							for(int i = 0; i < args.length; i++) {
		LetterFrequency fl = new LetterFrequency(arg[i]);
		fl.run(); 
							}
					   }	
					   else {		 // no arguments, read the URL from the file
		Scanner scan = new Scanner(new File("urlreadsta.txt"));  
		while(scan.hasNextLine())
		  {  
			String url = scan.nextLine();
				 LetterFrequency fl = new LetterFrequency(arg[i]);
				 fl.run(); 
		}  
   
		   }
}


Was This Post Helpful? 0
  • +
  • -

#11 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 11:00 AM

good news-ish, i kinda got it to work, i am having some issues because i still get exceptions, it processes some of the urls, but then stops running and exits due to exceptions.

also, i do not know how to store each url as it is processed and then output the total chracter counts for all urls processed.

any ideas?

here is my code now:

import java.util.Scanner;
import java.io.*;  

public class LetterFreq 
{	
	public static void main(String[] args)throws FileNotFoundException, IOException, InterruptedException
	 {  
		  
		Scanner scan = new Scanner(new File("urlreadsta.txt"));  
		while(scan.hasNextLine())
		  {  
			String url = scan.nextLine();
				System.out.println("Processing... " + url);
				Thread.sleep(3000);
				if (args.length == 0)
				{					
					url = scan.nextLine();
				}  
	 			else   
 				{   
	   			url = args[0];  
			   }	 
 
				LetterFrequency fl = new LetterFrequency(url);
				fl.run(); 
		}  
   
	}
}




the txt file

http://www.hotmail.com
http://www.google.com
http://www.mbusa.com
http://www.dreamincode.net



and the output/exceptions

 ----jGRASP exec: java LetterFreq

Processing... 
0: 85
1: 79
2: 75
3: 89
4: 22
5: 79
6: 28
7: 12
8: 31
9: 4
A: 2
B: 23
C: 14
D: 18
E: 7
F: 35
G: 7
H: 5
I: 24
J: 9
K: 4
L: 22
M: 14
N: 6
O: 3
P: 11
Q: 4
R: 8
S: 20
T: 12
U: 9
V: 5
W: 8
X: 5
Y: 3
Z: 4
a: 118
b: 24
c: 71
d: 51
e: 163
f: 59
g: 43
h: 43
i: 145
j: 8
k: 14
l: 103
m: 60
n: 106
o: 101
p: 108
q: 7
r: 172
s: 153
t: 160
u: 43
v: 84
w: 61
x: 20
y: 29
z: 4
Processing... http://www.google.com
0: 1
L: 1
R: 2
U: 1
a: 4
b: 1
c: 2
d: 4
e: 8
f: 1
h: 7
i: 2
l: 2
m: 5
n: 3
o: 3
p: 2
q: 1
r: 1
s: 2
t: 9
u: 2
v: 1
w: 3
x: 1
Processing... http://www.dreamincode.net
Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Unknown Source)
	at LetterFreq.main(LetterFreq.java:17)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 11:24 AM

What are you still doing with arg[0] ? You really have problem with that concept.

import java.util.Scanner;
import java.io.*;  

public class LetterFreq 
{	
	public static void main(String[] args)throws FileNotFoundException, IOException, InterruptedException
	 {  
		  
		Scanner scan = new Scanner(new File("urlreadsta.txt"));  
		while(scan.hasNextLine())
		  {  
			String url = scan.nextLine();
			System.out.println("Processing... " + url);
			Thread.sleep(3000);	 
 
			LetterFrequency fl = new LetterFrequency(url);
			fl.run(); 
		}  
   
	}
}




Works for me....
I had to comment the two lines

// LetterFrequency fl = new LetterFrequency(url);
// fl.run();

because I don't have your LetterFrequency class
Was This Post Helpful? 0
  • +
  • -

#13 scholio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 11-February 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 02:07 PM

sorry, i believe my letterfrequency was posted earlier, anyways here it is.

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

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));
  } 

} 




also, pbl, could explain why you advise against arg[0] please...

thanks

same question as before, how do i store all process url data and then for the last output, print the total sum of each character from each url processed?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 02:22 PM

View Postscholio, on 9 May, 2008 - 02:07 PM, said:

also, pbl, could explain why you advise against arg[0] please...

thanks


The String arg[] is used to pass a parameters to a Java application
What parameter are you passing to your application ?
May be you want to pass the filename so in that case:

public static void main(String[] arg) {

	 String filename = "ulrfile.dat";
	 if(arg.length > 0)
	   filename = arg[0];

	 // then open filename with the scanner
} 



So you can run the application passing to it tthe filename to use, if there are no parameter it will use the default "urlfile.dat"

In your case:

	 Scanner scan = new Scanner(new File("urlreadsta.txt"));  
		while(scan.hasNextLine())
		  {  
			String url = scan.nextLine();
				System.out.println("Processing... " + url);
				Thread.sleep(3000);
				if (args.length == 0)
				{					
					url = scan.nextLine();
				}  
				 else   
				{   
				   url = args[0];  
			   }	 

		}  



Calling your program with "foo.txt"
You would have open "urlreadsta.txt" anyway
scan it
and if ulreadstat.txt had 10 lines
set url = "foo.txt" 10 times
and call the constructor of LetterFrequency passing to it the String "foo.txt" 10 times
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: url character reader, compile help, reading data from .txt file

Posted 09 May 2008 - 02:35 PM

View Postscholio, on 9 May, 2008 - 02:07 PM, said:

same question as before, how do i store all process url data and then for the last output, print the total sum of each character from each url processed?


Change your run() method from void to Map<Character, Integer>

Map<Character,Integer> run() throws IOException {

At the end of your run method return theCounts

   DisplayResults();
   return theCounts;
}



In your LetterFreq build a Map<Character,Integer> total = new Map<Character, Integer>();

so after

	LetterFrequency fl = new LetterFrequency(url);
	Map<Character,Integer> tmp = fl.run();
// now scan tmp and add the count for every character into your Map total


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2