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

.