Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to find a way to grab the last three letters of a random length string so

ID: 3544810 • Letter: I

Question

I need to find a way to grab the last three letters of a random length string so I can use them in a switch statement.


The problem is :

Write a program that reads a web address (for instance, www.yahoo.com) from the keyboard and outputs whether this web address is for a government, a university, a business, an organization, or another entity.

If the web address contains .gov it is government, if it contains .edu it is university, if it contains .com it is business, if it contains .org it is organization, and if it contains anything other than that it is from another entity.


I can only use a switch statement for this problem which I have no problems with. My only problem is finding out a way to grab the last three letters (i.e. gov, edu, com, or org) to use in that switch statement.



Explanation / Answer

Using the below code, we can get the last 3 chars of your web address, use the same in the case statement conditions.

System.out.println("Enter the website address");

BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); //input buffer

String word = b.readLine();

int length = word.length();

String substr = word.substring(word.length() - 3)