Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,243 Java Programmers. There are 2,250 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

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

2 Pages V  1 2 >  
Reply to this topicStart new topic

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

scholio
post 8 May, 2008 - 02:51 PM
Post #1


New D.I.C Head

*
Joined: 11 Feb, 2008
Posts: 29



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
CODE


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

CODE

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:

CODE

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:
CODE

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
CODE

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


and my program so far:

CODE

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

CODE


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:

CODE


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

CODE

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: 8 May, 2008 - 08:50 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


pbl
post 8 May, 2008 - 08:59 PM
Post #2


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

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

CODE

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: 8 May, 2008 - 09:24 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

scholio
post 8 May, 2008 - 09:06 PM
Post #3


New D.I.C Head

*
Joined: 11 Feb, 2008
Posts: 29

QUOTE(pbl @ 8 May, 2008 - 08:59 PM) *

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

CODE

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

CODE

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


thanks again

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 8 May, 2008 - 09:12 PM
Post #4


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

Just replace

Scanner scan = new Scanner(System.in);

by

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

in your loop for each line do:
CODE

int[] urlCount = new int[255];


then
CODE

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: 8 May, 2008 - 09:30 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

scholio
post 9 May, 2008 - 06:37 AM
Post #5


New D.I.C Head

*
Joined: 11 Feb, 2008
Posts: 29

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
CODE

urlCount[str.charAt(i))++;


thanks

____________________________________
EDIt

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

CODE


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

CODE

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

CODE


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: 9 May, 2008 - 06:53 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 9 May, 2008 - 06:49 AM
Post #6


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

QUOTE(scholio @ 9 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
CODE

urlCount[str.charAt(i))++;


thanks


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

java

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

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

scholio
post 9 May, 2008 - 07:46 AM
Post #7


New D.I.C Head

*
Joined: 11 Feb, 2008
Posts: 29

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: 9 May, 2008 - 07:51 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 9 May, 2008 - 09:01 AM
Post #8


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

[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]

CODE

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.
[/quote]

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.
[/quote]

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
[/quote]

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.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

scholio
post 9 May, 2008 - 09:23 AM
Post #9


New D.I.C Head

*
Joined: 11 Feb, 2008
Posts: 29

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

CODE

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

CODE

----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?

CODE

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

CODE

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

CODE

----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: 9 May, 2008 - 09:32 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 9 May, 2008 - 09:46 AM
Post #10


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

QUOTE(scholio @ 9 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?


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

CODE

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



CODE

// 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

CODE

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

CODE

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

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

scholio
post 9 May, 2008 - 11:00 AM
Post #11


New D.I.C Head

*
Joined: 11 Feb, 2008
Posts: 29

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:

CODE

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

               &nbs