import java.util.Scanner;
import java.util.regex.*;
import java.io.*;
import javax.swing.*;

/**
 * A program that parses a user-supplied URL
 */

public class URLParser {

   public static void main(String[] args) throws IOException {
     Scanner scan = new Scanner(System.in);
     PrintWriter out = new PrintWriter(new FileWriter("D:\\cs2200\\lab8\\URLs.txt"));
     String input="";
// Everything works the way I want it to when I take out this while loop.
     while (input != "x") {
     System.out.print("Enter URL(type \"x\" to finish): ");
     input = scan.nextLine();
     parse(input);
     if (input.charAt(0) != 'N')
       out.println(input);
     System.out.println(input);
     }

     out.close();
   }

   public static String parse(String theInput) {
     
     boolean legality = false;
     theInput.trim();
     
     Pattern protocol = Pattern.compile("(http://)|(https://)|(ftp://)");
     Matcher matchProt = protocol.matcher(theInput);
     boolean foundProtMatch = matchProt.find();
     
     Pattern hostname = Pattern.compile("(www.)|([a-zA-Z0-9]{1,}\\.)");
     Matcher matchHost = hostname.matcher(theInput);
     boolean foundHostMatch = matchHost.find();
     
     Pattern path = Pattern.compile("[/\\.a-zA-Z0-9]*");
     Matcher matchPath = path.matcher(theInput);
     boolean foundPathMatch = matchPath.find();
     
     if (theInput.charAt(0) == 'h' || theInput.charAt(0) == 'f') {
       if ( foundProtMatch == true && foundHostMatch == true && foundPathMatch == true ) {
         JOptionPane.showMessageDialog(null, "Yes, it's legal."); 
         legality = true; }
       else {
         JOptionPane.showMessageDialog(null, "No, it’s illegal.");
         legality = false; }
     }
     
     else if (theInput.charAt(0) == 'w' && theInput.charAt(1) == 'w' && theInput.charAt(2) == 'w') {
       if ( foundHostMatch == true && foundPathMatch == true ) {
         JOptionPane.showMessageDialog(null, "Yes, it's legal.");
         legality = true; }
       else {
         JOptionPane.showMessageDialog(null, "No, it’s illegal.");
         legality = false; }
     }
     else {
       JOptionPane.showMessageDialog(null, "No, it’s illegal.");
       legality = false; }
     
     if (legality == false)
       theInput = "Not Legal: " + theInput;
//     System.out.println(theInput);
     return theInput;
   }

   private String trim(String theInput) {
     theInput.trim();
     return theInput;
   }
}

Any pointers as to what is exactly happening in your application and what it is supposed to do or what errors you are getting would be nice.

Now if you are referring to this :-

while (input != "x") {

Then I wish to inform you that in Java you cannot compare strings directly with "==" or "!=", you need to use the equals(String) or equalsIgnoreCase(String) method of the String class.


Also "==" and "!=" would only check if the two references point to the same object as in the below case :-

String str1="Alpha";
String str2=str1;

In the above case (str1==str2) would return true. But consider the following case :-

String str1="Alpha";

// Using this constructor inorder to stop the java compiler from optimizing.
String str2 = new String("Alpha");

In this case, (str1!=str2) would return true, As they would be referring to different objects. However str1.equals(str2) or str1.equalsIgnoreCase(str2) or vice versa would always reurns true as their "contents" are the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.