• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

URL encoding and arguments

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok heres my problem:
I am suppose to be creating a class for java where you enter a URL, like www.verizon.com, and by asking for arguments be able to create a complete URL, like http://www.verizon.com?id=44483&account=yu42hh.
I created the class and was already given the base code that the class worked in. My problem is getting the ? and & to go into the right places by using a counter instance variable to count the arguments.
The class code looks like this:

public class MyUrl
{
private String mUrl;

public MyUrl(String url) {
if (url != "http://")
{
mUrl = "http://" + url + '?';
}
else
{
mUrl = url + '?';
}
}
public void addArgument(String name, String value) {
String oName = "";
String oValue = "";
int lName = name.length();
int lValue = value.length();
char[] chars = name.toCharArray();
for (int x = 0; x < lName; x++)
{
char a = chars[x];
String hexValueN = Integer.toHexString(a);
if (a == ' ')
oName += '+';
else if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || (a >= '0' && a <= '9')
|| (a == '_') || (a == '-') || (a == '.') || (a == '*'))
oName += a;
else
oName += '%' + hexValueN;
}
name = oName;
mUrl += oName + '=';
char[] charss = value.toCharArray();
for (int y = 0; y < lValue; y++)
{
char b = charss[y];
String hexValueV = Integer.toHexString(b);
if (b == ' ')
oValue += '+';
else if ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
|| (b == '_') || (b == '-') || (b == '.') || (b == '*'))
oValue += b;
else
oValue += '%' + hexValueV;
}
value = oValue;
mUrl += oValue + '&';
}
public void addArgument(String name, int ivalue) {
String oName = "";
int lName = name.length();
char[] chars = name.toCharArray();
for (int x = 0; x < lName; x++)
{
char a = chars[x];
String hexValueN = Integer.toHexString(a);
if (a == ' ')
oName += '+';
else if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || (a >= '0' && a <= '9')
|| (a == '_') || (a == '-') || (a == '.') || (a == '*'))
oName += a;
else
oName += '%' + hexValueN;
}
name = oName;
mUrl += oName + '=';
mUrl += Integer.toString(ivalue);
}
public void addArgument(String name, double dvalue) {
String oName = "";
int lName = name.length();
char[] chars = name.toCharArray();
for (int x = 0; x < lName; x++)
{
char a = chars[x];
String hexValueN = Integer.toHexString(a);
if (a == ' ')
oName += '+';
else if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || (a >= '0' && a <= '9')
|| (a == '_') || (a == '-') || (a == '.') || (a == '*'))
oName += a;
else
oName += '%' + hexValueN;
}
name = oName;
mUrl += oName + '=';
mUrl += Double.toString(dvalue);
}
public String toString() {
return mUrl;
}
public static String urlEncode(String text) {
String oText = "";
int lText = text.length();
for (int z = 0; z < lText; z++)
{
char c = text.charAt(z);
String hexValueT = Integer.toHexString(c);
if (c == ' ')
oText += '+';
else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|| (c == '_') || (c == '-') || (c == '.') || (c == '*'))
oText += c;
else
oText += '%' + hexValueT;
}
text = oText;
return text;
}
}

What I need help with is I am suppose to be using the public static String urlEncode(String text) code within the public void addArgument codes to URL encode the name and value and be able to create a variable for each set of arguments which I can then use to make a loop to decide where to put the ? and & in the finished URL. As you can see right now I am just kind of adding a ? and & in places in the URL to make it look right, but it doesnt work correctly considering I could end up with a string that has a & at the end of the URL, which isnt right (something like ...&id=44632& at the end of the URL).

I am not that great of a coder and for an introductory class this teacher goes above and beyond what he describes in the notes and the book we have to make, at least to me, some really hard assignments.

So could someone possibly help me figure out how to put the public static String urlEncode(String text) code within the public void addArgument codes to URL encode the name and value and be able to create a variable for each set of arguments which I can then use to make a loop to decide where to put the ? and & in the finished URL?

Thank you so much whoever might help me on this, I dont know if this is asking too much, so go easy on me please .
 
Marshal
Posts: 78653
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please find the code tags when you post; they make quoted code much easier to read.

There are several places where you could improve your code and get better marks.
  • It is unnecessary to create an int variable for a for-loop. Use . . . i < array.length; . . .
  • For Strings (see link below) you can avoid creating a char[] array by using the charAt() method which you are using elsewhere.
  • Similarly, if you have a char[] array there is no need to copy a member into a local variable. Use array instead.
  • You have 5 if statements which appear almost identical to one another. This looks to me like a method which you ought to be calling rather than writing the same thing 5 times.
  • Go through the Character class. There are methods like isLetter() which might make your if statements easier to write.
  • One error which will cause you major problems. Avoid using == to check whether objects are identical, and that applies to Strings. Use their equals() method. The use of == in the constructor is almost certain to produce wrong results. And check whether you really want [I]equals as opposed to startsWith or beginsWith, which you will find in the String class, only I can't remember the correct spelling just at the moment.

    To call the urlEncode method, you simply say something likeWhat that does is to send the "text" parameter on to the urlEncode method, then the urlEncode method creates a String from it, then it passes the String back for further manipulation.

    I can't run your class because it is only part of your application, so I don't know whether you are getting the % & or ? in the right places. I think you are going to have to do it the hard way. Get a large sheet of paper and write
  • www.verizon.com
  • www.verizon.com?
  • www.verizon.com?id
  • www.verizon.com?id=
  • www.verizon.com?id=12345
  • www.verizon.com?id=12345&
  • www.verizon.com?id=12345&account
  • www.verizon.com?id=12345&account=
  • www.verizon.com?id=12345&account=abc123
  • See what stages you go through to go from one to the next. Can you add those bits in groups, eg adding ?id= as one String?

    Good luck with it.
    [ March 01, 2008: Message edited by: Campbell Ritchie ]
     
    Ryan Speller
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This is the code that the class runs in



    We havent got to arrays yet, but I looked ahead and just decided to use it. Still a bit lost though, so if you could clarify from what Ive given you here would be a huge help!
     
    Ryan Speller
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    O and to make it easier heres the code from the class I am to create using the correct format for the boards.

     
    Campbell Ritchie
    Marshal
    Posts: 78653
    374
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It is still not elegant; when I tried it on Eclipse it said "parameter name should not be assigned." That means you are putting name to the left of an = assignment operator; you ought to leave the parameter's value unchanged.

    It seems to produce the values wanted, only with an & at the end. Suggest you add your & before the arguments, if the existing string does not end with ?. You will find String class methods which allow you to check for that a String ends with. Try that, see whether it improves it.
     
    Of course, I found a very beautiful couch. Definitely. And this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic