Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2012, 05:18 AM   PM User | #1
alisxx
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
alisxx is an unknown quantity at this point
hep with simple URL encoding a String program

I wrote this code for encoding a string into URL

Code:
package URLencode;

import java.util.Scanner;

public class URLencode {
    
    private static final String notEnc =
        "abcdefghijklmnopqrsuvwxyz" +
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
        "0123456789.*_-";
    private static final char[] hexChar = {
        '0', '1', '2', '3', '4', '5',
        '6', '7', '8', '9', 'A', 'B',
        'C', 'D', 'E', 'F'
    };
        
        public static void main (String args[])
    {
        Scanner keyboardScanner = new Scanner (System.in);
        System.out.println("Enter a line of text to be URL encoded...");
        String text = keyboardScanner.nextLine();
        System.out.println("The URL entered is: " + text);
        System.out.println("The length of chars of URL entered is: " +     text.length());
        StringBuilder result = new StringBuilder(text.length()); 
        for (int n = 0; n < text.length(); ++n) {
            char c = text.charAt(n); 
            System.out.println("The encoded URL is: " + text);
            System.out.println("The length of chars is: " + text.length());
            if (notEnc.indexOf(c) != -1) {
                StringBuilder append = result.append(c);
            }
            else if (c == ' ') {
                    result.append('+');
            }
            else
            {
            String hexValue = Integer.toHexString(c); 
            StringBuilder append = result.append('%');
            result.append(hexChar[(c >> 4) & 0xF]);
            result.append(hexChar[c & 0xF]);
            }
         }  
       }  
    }
but it does not substitute '+' for the blanks and it loops many times.??

here are 2 sample outputs but I cannot get it to do that

Enter a line of text to be URL encoded
This should have plus symbols for blanks
The string read is: This should have plus symbols for blanks
Length in chars is: 40
The encoded string: This+should+have+plus+symbols+for+blanks
Length in chars is: 40

Enter a line of text to be URL encoded
This should have hex 2f for /
The string read is: This should have hex 2f for /
Length in chars is: 29
The encoded string: This+should+have+hex+2f+for+%2f
Length in chars is: 31
alisxx is offline   Reply With Quote
Old 02-14-2012, 03:24 PM   PM User | #2
alisxx
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
alisxx is an unknown quantity at this point
Thanks in advance.
alisxx is offline   Reply With Quote
Old 02-14-2012, 05:13 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is too much work honestly. You don't need to use hexChar at all, you can just use the string formatter to display it as a hex:
PHP Code:
        for (int n 0text.length(); ++n)
        {
            
char c text.charAt(n);
            if (
notEnc.indexOf(c) != -1)
            {
                
result.append(c);
            }
            else if (
== ' ')
            {
                
result.append("+");
            }
            else
            {
                
result.append("%" String.format("%h"c));
            }
        } 
You can also use the URLEncoder class for a single line call.
Fou-Lu is offline   Reply With Quote
Old 02-14-2012, 05:25 PM   PM User | #4
alisxx
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
alisxx is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
This is too much work honestly. You don't need to use hexChar at all, you can just use the string formatter to display it as a hex:
PHP Code:
        for (int n 0text.length(); ++n)
        {
            
char c text.charAt(n);
            if (
notEnc.indexOf(c) != -1)
            {
                
result.append(c);
            }
            else if (
== ' ')
            {
                
result.append("+");
            }
            else
            {
                
result.append("%" String.format("%h"c));
            }
        } 
You can also use the URLEncoder class for a single line call.
Really so hexChar class object should be deleted completely ? I see your code for substituting blanks for + is the same as mine, but mine was not working - it is not substituting the blanks - was it because my else statement after it was wrong?
alisxx is offline   Reply With Quote
Old 02-14-2012, 07:31 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No, that one in particular is simply because result was never shown. All you'd ever see was the original string.
Edit:
As for hexChar, that's up to you. You can either use String.format, or you can use bit shifting and masking to display each character within the hexChar. The calculation you have for that is correct from the looks of it.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:04 AM.


Advertisement
Log in to turn off these ads.