Question

in java In a class called DomainParser, write a method getTopLevelDomain that accepts a URL in...

in java

In a class called DomainParser, write a method getTopLevelDomain that accepts a URL in String format then returns the top level domain.

Files in the same directory:

ComDemo.java

Standard Input:

google.com

Required Output

Enter a String\n
Top level domain: com\n

Standard Input

google.co.in

Required Output

Enter a String\n
Top level domain: in\n
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

class ComDemo {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a String");
        String line = keyboard.nextLine();
        System.out.println("Top level domain: " + DomainParser.getTopLevelDomain(line));
    }
}

class DomainParser {
    public static String getTopLevelDomain(String domain) {
        int index = domain.lastIndexOf('.');
        if (index == -1) {
            return "";
        } else {
            return domain.substring(index + 1);
        }
    }
}

Enter a String google.com Top level domain: com Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
in java In a class called DomainParser, write a method getTopLevelDomain that accepts a URL in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT