Question

In: Computer Science

Run the program and enter domain names to validate. Note that even valid input is flagged...

  1. Run the program and enter domain names to validate. Note that even valid input is flagged as invalid.
  2. Change the program to validate a domain name. A valid domain name for this program has a second-level domain followed by a core gTLD. Run the program again.

    import java.util.Scanner;

    public class CoreGtldValidation {   
    public static void main (String [ ] args) {
    Scanner scnr = new Scanner(System.in);
    String coreGtld1;
    String coreGtld2;
    String coreGtld3;
    String coreGtld4;
    String inputName;
    String searchName;
    String theTld;
    boolean isCoreGtld;
    // FIXME: Add variable periodCounter to count periods in a domain name
    int periodPosition; // Position of the period in the domain name
    int i;

    coreGtld1 = ".com";
    coreGtld2 = ".net";
    coreGtld3 = ".org";
    coreGtld4 = ".info";
    theTld = "";
    isCoreGtld = false;
    periodPosition = 0;

    System.out.println("\nEnter the next domain name (<Enter> to exit): ");
    inputName = scnr.nextLine();

    while (inputName.length() > 0) {
    searchName = inputName.toLowerCase();
    isCoreGtld = false;

    // FIXME: Write a for loop using variable i to store in periodCounter
    // the number of periods in searchName. Store the position of the
    // period in periodPosition. If searchName has exactly one period
    // and searchName's first character is not a period, determine
    // whether searchName has a valid core gTLD by extracting the
    // domain name into variable coreGtld and comparing the name with
    // valid core gTLDs.

    // Extract the top-level domain name starting at the period's position.
    // Ex: If searchName = "example.com", the next statement extracts ".com"
    theTld = searchName.substring(periodPosition);

    if (theTld.equals(coreGtld1)) {
    isCoreGtld = true;
    }
    else if (theTld.equals(coreGtld2)) {
    isCoreGtld = true;
    }
    else if (theTld.equals(coreGtld3)) {
    isCoreGtld = true;
    }
    else if (theTld.equals(coreGtld4)) {
    isCoreGtld = true;
    }
    else {
    isCoreGtld = false;
    }

    System.out.print("\"" + inputName + "\" ");
    if (isCoreGtld) {
    System.out.println(" is a second-level domain followed by a core gTLD of \"" +
    theTld + "\"");
    }
    else {
    System.out.println("is not a second-level domain followed by a core gTLD.");
    }

    System.out.println("\nEnter the next domain name (or <Enter> to exit): ");
    inputName = scnr.nextLine();
    }
    }
    }

Solutions

Expert Solution

COMPLETED JAVA CODE:(CoreGtldValidation.java)

import java.util.Scanner;

public class CoreGtldValidation {
public static void main (String [ ] args) {
Scanner scnr = new Scanner(System.in);
String coreGtld1;
String coreGtld2;
String coreGtld3;
String coreGtld4;
String inputName;
String searchName;
String theTld;
boolean isCoreGtld;

int periodCounter=0; // variable periodCounter to count periods in a domain name
int periodPosition; // Position of the period in the domain name
int i;

coreGtld1 = ".com";
coreGtld2 = ".net";
coreGtld3 = ".org";
coreGtld4 = ".info";
theTld = "";
isCoreGtld = false;
periodPosition = 0;

System.out.println("\nEnter the next domain name (<Enter> to exit): ");
inputName = scnr.nextLine();

while (inputName.length() > 0) {
searchName = inputName.toLowerCase();
isCoreGtld = false;

// FIXME: Write a for loop using variable i to store in periodCounter
// the number of periods in searchName. Store the position of the
// period in periodPosition.

for(i=0;i<searchName.length();i++)
{
   if(searchName.charAt(i)=='.')
   {
       periodCounter++;
       periodPosition=i;
   }
  
}
// If searchName has exactly one period
// and searchName's first character is not a period, determine
// whether searchName has a valid core gTLD by extracting the
// domain name into variable coreGtld and comparing the name with
// valid core gTLDs.
// Extract the top-level domain name starting at the period's position.
// Ex: If searchName = "example.com", the next statement extracts ".com"

if(searchName.charAt(0)!='.' && periodCounter ==1)
  
theTld = searchName.substring(periodPosition);

if (theTld.equals(coreGtld1)) {
isCoreGtld = true;
}
else if (theTld.equals(coreGtld2)) {
isCoreGtld = true;
}
else if (theTld.equals(coreGtld3)) {
isCoreGtld = true;
}
else if (theTld.equals(coreGtld4)) {
isCoreGtld = true;
}
else {
isCoreGtld = false;
}

System.out.print("\"" + inputName + "\" ");
if (isCoreGtld) {
System.out.println(" is a second-level domain followed by a core gTLD of \"" +
theTld + "\"");
}
else {
System.out.println("is not a second-level domain followed by a core gTLD.");
}

System.out.println("\nEnter the next domain name (or <Enter> to exit): ");
inputName = scnr.nextLine();
}
}
}

SCREENSHOT FOR OUTPUT:


Related Solutions

Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
Create a Java program that asks a user to enter two file names. The program will...
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist. first input is the name of the first file and it has 2 (length) 4 5 6 7 Second input is the name of the second file and it has 2 (length) 6 7 8 9 try catch method
C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
You are off to shopping and need a program that allows you to enter the names...
You are off to shopping and need a program that allows you to enter the names of items, their price and quantities. Here is what a sample run should look like (with the keyboard input shown in italics) ... Enter item information ("exit" to exit) item 1: chips price: 3.5 quantity: 2 Enter item information ("exit" to exit) item 2: red wine price : 15.0 quantity : 1 Enter item information ("exit" to exit) item 3 : steaks price :...
Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Using RAPTOR create a program that allows the user to input a list of first names...
Using RAPTOR create a program that allows the user to input a list of first names in on array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. the output should be a list of email address where the address is of the following form: [email protected]
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Create program which verifies if input string is a valid variable declaration or not. Use C...
Create program which verifies if input string is a valid variable declaration or not. Use C programming language. - This program can only use the following variable types: char, float, and int - Remove any newline \n from input string - The input prompt should say ">>> " - If input declaration is valid, it should print "Valid dec\n" - If input declaration is invalid, it should print "Invalid dec\n" - Make sure the identifier entered matches the rules of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT