Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Creating a google search to create a URL

I originally thought I had this one... Use scanner, get input from user, then concatenate it to a google search. My teacher stated the follow rules for URL's:
1. Replace +'s with %2B
2. Replace all spaces with +'s
3. Replace all "'s with %22
So here I am, doing what I thought was said, and obviously doing something wrong. I can just add the input to the end of the google link but am need to make a proper URL. Below is what I have, and thank you in advance for your help. As I said, I can just put the ("http://www.google.com/?q + one") and it works.
Java:
public class googleassignment {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("What would you like to search for:");
        String one = scan.nextLine();
        System.out.println(%22https://www.google.com/?q="+%2B+one%22);
        
    }

}
 
Have you not tried to compile that code ? What do the errors tell you ?
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token(s), misplaced construct(s)
Syntax error on token "22", ? expected after this token
Syntax error on token ":", Expression expected after this token

at googleassignment.main(googleassignment.java:5)
Hey, thank you for your help; You've helped me before.... So, with my java program without replacing the +'s, or spaces, or (")'s gives me the URL of https://www.google.com/?q= and then whatever I asked the user to input. But I think the teacher wants a "clickable" link? Are the replacements necessary? Using Eclipse.
 
Yes, the replacements (or rather, escapes as these are called) are needed. But only for the URL (as was stated), not in the Java code, as you did with the quotes. An URL must not contain spaces or quotes, and the + sign is interpreted as a parameter separator. So if you have any of these characters in the hostname or in a parameter, they must be escaped. Incidentally, this also holds for question marks, which your teacher probably should have mentioned.
You should check your generated URL by pasting it in a browser.
To make it really clickable you'll need to write a Java GUI program with an onClick event handler. That is probably not what is required from you right now. But when in doubt about the requirements, ask the teacher !
 

Buy us a coffee!

Back
Top Bottom