WIkipedia pages have a specific webpage address. Any address starts with https:/
ID: 3747343 • Letter: W
Question
WIkipedia pages have a specific webpage address. Any address starts with https://en.wikipedia.org/wiki/ and then continues with the topic for that page e.g. Computer_science, Java_(programming_language). See examples below:
https://en.wikipedia.org/wiki/Computer_science
https://en.wikipedia.org/wiki/Java_(programming_language)
https://en.wikipedia.org/wiki/Imagine_Dragons
Write a program that reads a string from the user. If it is a valid wikipedia webpage, it extracts and prints the topic for that page. If it is not a valid address it prints the message Not a valid wikipedia webpage address.
Hint: How can the fact that all valid addresses start with the same text before the topic name help you extract the topic name?
You do NOT need to handle the case where the webpage address is correct, but it is written in uppercase letters. You can assume it is all lowercase.
the text is all in lowercase and
if the address is valid, then it will have at least one more character for the topic. That is, you do not need to handle an input like: https://en.wikipedia.org/wiki/.
---- Sample run 1
This program will extract the topic from a valid Wikipedia webpage address.
Enter a web address: https://en.wikipedia.org/wiki/Computer_science
Topic: Computer_science
Bye.
---- Sample run 2
This program will extract the topic from a valid Wikipedia webpage address.
Enter a web address: http://vlm1.uta.edu/~alex/courses/1310/homework/hw02.html
Not a valid wikipedia webpage address.
Bye.
---- Sample run 3
This program will extract the topic from a valid Wikipedia webpage address.
Enter a web address: https://en.wikipedia.org/wiki/
Topic:
Bye.
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// WikiCheck.java
import java.util.Scanner;
public class WikiCheck {
// all valid addresses start with this prefix
static String PREFIX = "https://en.wikipedia.org/wiki/";
public static void main(String[] args) {
// scanner to read input
Scanner scanner = new Scanner(System.in);
// prompting and receiving the address from user
System.out.println("This program will extract the topic "
+ "from a valid Wikipedia webpage address.");
System.out.print("Enter a web address: ");
String address = scanner.nextLine();
// checking if address, in lowercase starts with the prefix
if (address.toLowerCase().startsWith(PREFIX)) {
// valid wikipedia address.
// getting topic, using substring() method. It will slice the text
// between the indices PREFIX.length() to end of text and returns it
String topic = address.substring(PREFIX.length());
//displaying topic
System.out.println("Topic: " + topic);
} else {
// invalid wikipedia address.
System.out.println("Not a valid wikipedia webpage address.");
}
System.out.println("Bye.");
}
}
/*OUTPUT 1*/
This program will extract the topic from a valid Wikipedia webpage address.
Enter a web address: https://en.wikipedia.org/wiki/Computer_science
Topic: Computer_science
Bye.
/*OUTPUT 2*/
This program will extract the topic from a valid Wikipedia webpage address.
Enter a web address: www.google.com/abcdef
Not a valid wikipedia webpage address.
Bye.
/*OUTPUT 3*/
This program will extract the topic from a valid Wikipedia webpage address.
Enter a web address: https://en.wikipedia.org/wiki/
Topic:
Bye.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.