Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks in advance.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •