aspose file tools*
The moose likes Beginning Java and the fly likes pulling my hair out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pulling my hair out " Watch "pulling my hair out " New topic
Author

pulling my hair out

Steve Cooke
Greenhorn

Joined: Feb 15, 2006
Posts: 8
ok, so i sit wondering why i spent all this money on this class just to treated like a tool for not getting it. have to love teachers and other classmates when you try to get help from them and they talk to you in riddles.

ok so i've been trying to get help in class for like a week on this program just stare at this thing with no clue what so ever. The book we have is pointless and the other books i have don't look like this code we have to add on to.



so here is the code. I know how to take care of the encoding part to a degree. well atleast do the url encoding and i'm pretty sure i can get that part working ok with some trial and error. but with the MyUrl section i have no clue where to start and just don't get what i'm getting told in class. It's like they are talking in riddles and i'm off to find the oak tree with the broken branch. make a left, walk 100 paces to the creek, etc.

so what is suppose to happen is:

Enter URL site (or 'exit)
www.amazon.com
Url value read was: www.amazon.com
Enter URL argument name (or 'done')
id
Enter type of argument value (string, integer, double)
integer
Enter an integer value
123
Enter URL argument name (or 'done')
author
Enter type of argument value (string, integer, double)
string
Enter a string value
Jim Campbell
Enter URL argument name (or 'done')
publisher
Enter type of argument value (string, integer, double)
string
Enter a string value
O'Reilly
Enter URL argument name (or 'done')
done
URL with appended arguments is:
http://www.amazon.com/?id=123&author=Jim+Campbell&publisher=O%27Reilly

Enter URL site (or 'exit')
exit

the appended section i can understand. But how to start this i'm lost. Perhaps some examples maybe, nothing related to my assignment because i really want to learn this but each week i'm becoming more discouraged from the lack of help in class.

Thanks for any help. This noob here can use it.
Steve
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10

It looks like the first thing you need to do is create a class named MyUrl that has a constructor which takes a String as a parameter. Do you understand how to do that?

The next thing your new MyUrl class needs is a method named addArgument which takes two Strings as parameters. Do you understand how to do that?

If so, let's see your code up to that point, and we'll go from there.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Marilyn has given some decent advice to get started. If you don't understand the terminology, then you should read your textbook or research it further online. Or you can ask specific questions to help us know what you need claified. In fact, it will help a lot if you can describe where you are stuck. In particular, what are you supposed to do for this homework assignment?

Also, please take a moment to look at these suggestions for asking a question. If you follow the advice given, you will greatly increase the chances of getting help.

Layne


Java API Documentation
The Java Tutorial
dave taubler
Ranch Hand

Joined: May 15, 2001
Posts: 132
Hi Steve,

I can sympathize. I've known Java for many years know (and these days, it's my favorite programming language by far), but I remember feeling so frustrated and helpless when I was trying to learn it.

Can you give a brief overview of where you're at in the class, and also how you wound up with the code snippet you provided inyour opening post (e.g. did the instructor give it to you and ask you to fill in the missing parts? is it your first attempt at completing an assignment?)

Oh, and if it makes you feel any better, it looks like the whole command-line interaction of that code snippit is pretty a-typical of Java anyhow...


Dave Taubler<br />Specializing in <a href="http://taubler.com/articles/" target="_blank" rel="nofollow">Java and Web Development</a>
Steve Cooke
Greenhorn

Joined: Feb 15, 2006
Posts: 8
Thanks all for the help thus far. The opening code is what the instructor has provided for us. We are to write a class called MyUrl to statisfy the program.

I ended up staring at this more and more last night until it all just looked the same but so far have come up with this.



the book i have to me isn't very good compaired to other books i have for programming in other languages. the book we had to get shows like step 1, 2, 3, 34, 56 98. then step 34 refers back to step 5, 8, 12. but those steps come later in the chapter. when you jump to that point in the chapter it just shows those steps but not how it would interact with the rest of the code. so it makes it really hard to follow.

The encoding part i know how to do, we had a similar assignment on that. my question now would be with what i have started so far, the code runs though everything that it should, but as i shown above an example of how the output would work. when the URL argument name, type and value is added, this i know will work if i put in one, but if there are multiples like above will the main demo code add on to the existing or do i still have to write another statement for that. sorry if this sounds a bit confusing. I'm seeing what needs to be done and slowing getting it as i go. I get the light bulb "oh yeah, that's how it goes" as i'm working on this. I'm still writing my code but this is where i'm at, at this point.

so far in class we have covered Primitive types, strings and interactive i/o, looping and branching, classes, objects, and methods. our section on classes, objects and methods, the notes have been very vague same with the book. I picked up java for dummies and from what it shows is everything completely different than what my notes show and my other book. I'm comfortable with everything up looping and branching. classes objects and methods i understand them but not very comfortable with them.

Thanks again.
Steve
[ March 09, 2006: Message edited by: Steve Cooke ]
dave taubler
Ranch Hand

Joined: May 15, 2001
Posts: 132
One thing I should've asked as well is how familiar to object-oriented programming in general you are.

At any rate, you're off to a good start with the MyUrl class. So let's look at what you want to do with the toString() method. Based on what the UrlDemo class does, you're going to know that values "mbaseUrl" and "mName", definitely. The problem arises with msValue, mValue, and mdValue... based on the current MyUrl implementation, you're not going to know if the user entered a String, an int, or a double value by the time the toString() method is called.

So how do you solve it? I can think of a few ways to do so. It would be hard to say which one is the most "correct" way, since this is merely an exercise, and not a real-world application with real-world requirements. So in that case, I would go with the simplest solution.

I won't completely solve it for you, but in general, I'd say get rid of mValue and mdValue entirely, and use only msValue. So even if the user enters an int or a double value, you always save it as a String. That way, you don't even *need* to know whether the user had entered a String, an int, or a double by the time toString() is called.

See how that goes, and let me know.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

One thing I noticed is that you can have multiple attributes, of which they can be the same type. I think it may be better to store them in some sort of collection, instead of instance variables.

The way it is written now, sequential calls to addArgument() will step on each other.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
dave taubler
Ranch Hand

Joined: May 15, 2001
Posts: 132
Originally posted by Henry Wong:
One thing I noticed is that you can have multiple attributes, of which they can be the same type. I think it may be better to store them in some sort of collection, instead of instance variables.

The way it is written now, sequential calls to addArgument() will step on each other.

Henry

That's a good point. I was thinking that MyUrl is designed for one instance per URL (which is obviously correct), but you're right, it can have multiple arguments.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
One thing that I think will help is if you describe in words what the addArgument() method is supposed to do. This should help you get a handle on what you need to do to get it to work correctly. Perhaps it will also help if you describe what the MyUrl class represents as a whole.

Layne
Steve Cooke
Greenhorn

Joined: Feb 15, 2006
Posts: 8

One thing I noticed is that you can have multiple attributes, of which they can be the same type. I think it may be better to store them in some sort of collection, instead of instance variables.

The way it is written now, sequential calls to addArgument() will step on each other.

Henry


I think i see what your saying. having the multiple calls to addArgument in MyUrl can cause problems enven with then 2nd value either being string, int or double. I was thinking on something like a toString method on the double and int similar to what i did on a prev assignment but that's not going quite so well.

My overall understanding on OOP is pretty decent. I haven't touched codes in almost 2 years since my last programming classes. I've taken VB .NET for 2 semesters and C. I was pretty decent with C and not too bad with VB. I had taken C++ but ended up dropping the class because I was running into issues like now. Where the teacher was like here is the assignment and the class is like 'Huh?' No lectures, no books, nothing.

So what MyURl should be doing in my opinion is, to take the url that is entered, ex. www.yahoo.com and first change it to http://www.yahoo.com/. I have somewhat a grasp on this but haven't taken this on yet since i wanted to get the hard part (for me) out of the way. Then it should take info that is passed from MyUrlDemo, the names should be a string, if the value is a string, or int or double, return those values with the name, appending it to the url. then the url will be encoded after http://www.yahoo.com/ (this i can do and have a few ideas on this too.

also after http://www.yahoo.com/ a ? should be place then the arguments. in between each argument should be separated with &. So the url encoding should be done before it's appended to the Url string.

I did take out the other two addArguments and have been monkeying around with the value portion of it.

but i end up getting this error on MyUrlDemo in the snippet of code below.

The method addArgument(String, String in the type MyUrl is not applicable for the arguments (String, int) or
The method addArgument(String, String in the type MyUrl is not applicable for the arguments (String, double)



I know this will become much easier with time and practice, but for me right now this seems like the most difficult thing i've encounted in programming. Nothing like my C or VB .NET class, but then again in those classes, We had decent help

Thanks for all the help, or i'd still be pulling out my hair.
Steve Cooke
Greenhorn

Joined: Feb 15, 2006
Posts: 8
actually here is what i'm looking at right now. I may be a bit sloppy but so far to me it looks promising.



so what i'm looking at is assinging newUrl to mbaseUrl and return that value for the Url value read was:
also assigning newUrl to encodedURL and applying the http:// and / to the url.

for the addArgument, the values will still span for either string, int or double. if it's int or double they will be converted to string and in either case the values will be assigned to mValue. From here i would want to pass mName and mValue to static String urlEnocde to be encoded then append it to encodedUrl. Once the string toString is called, all it has to do is return encodedUrl and the encoded Url will be displayed.
Steve Cooke
Greenhorn

Joined: Feb 15, 2006
Posts: 8
ok here is where i'm stuck. the code to a point works



the only hint i got from class was to pass the value from the addArgument to the public static string to encode it.

i can get this to go, but it won't append to the mbaseUrl. is there a way to create like a global string that will save the values to and return that string when called on in the public string tostring(). sorry if this doesn't quite make any sense. I'm still going to clean up the code a bit and put the finishing touches on it, but this is where i'm stuck is on the return of the addArgument values.

if i add like id 123, then color blue.
colorblue will return instead of like www.yahoo.com/id123colorblue.

i'm going to be adding in the sections to put http:// and / for the url along with the ? after / in the url also adding the = for like id=123 along with the & to separeate each argument (id=123&color=blue)
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
It looks like you are very close to the solution.
The ideas you have so far are the same I use to complete the requirement. Keep on the same track and tell us how did you finish you assignment.
John Crowe
Greenhorn

Joined: Jan 02, 2009
Posts: 1
I am working on the same program, but my problem is specific to the static String urlEncode

I first tried importing the java.net.URLEncoder and as a result my method looked like this:

public static String urlEncode(String text)
{
return URLEncoder.encode( text );
}


But as it turns out it is a deprecated api, so instead I need to apply the static Integer.toHexString method to return the encoded value for characters in the url.

Here is my code:

import java.net.URLEncoder;

public class MyUrl
{

private String mUrl = null;


public MyUrl( String url )
{
if( url.startsWith( "http://" )) {
mUrl = url;
} else {
mUrl = "http://" + url;
}
}

public void addArgument(String name, String value)
{
String separator;
if( mUrl.contains( "?" )) {
separator = "&";
} else {
separator = "?";
}
mUrl = mUrl + separator + urlEncode( name ) + "=" + urlEncode( value );
}


public void addArgument(String name, int ivalue)
{
String value = Integer.toString( ivalue );
addArgument( name, value );
}

public void addArgument(String name, double dvalue)
{
String value = Double.toString( dvalue );
addArgument( name, value );
}


public String toString()
{
return mUrl;
}


public static String urlEncode(String text)
{
return URLEncoder.encode( text );
}
}


Any suggestions?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61430
    
  67

"JC",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pulling my hair out