aspose file tools*
The moose likes Beginning Java and the fly likes Counter Instance Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Counter Instance Variable" Watch "Counter Instance Variable" New topic
Author

Counter Instance Variable

Ryan Speller
Greenhorn

Joined: Mar 01, 2008
Posts: 9
Ok, I am about to flip. I already posted once with this code for this really bad defined assignment I was given and I need to figure out a way to count the number of arguments in my class I have created to decide where to put ? and & within a URL. My really vague professor says to use a counter instance variable that contains the number of arguments appended to the object's URL... I have no idea how to do this.

Here is the code that I have:


So how do I create a code to count the arguments that are appended to the URL to then figure out where to put ? and & in the URL?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

I already posted once with this code for this really bad defined assignment I was given and I need to figure out a way to count the number of arguments in my class I have created to decide where to put ? and & within a URL. My really vague professor says to use a counter instance variable that contains the number of arguments appended to the object's URL...


To be blunt, you are being "really vague" too.

What is the definition of "number of aurgments in [your] class"? Since when does a class have arguments? We have no idea what this is, much less know what it means to count it.

What does "put ? and & within a URL" have to do with a Java class? What the heck is an "object's URL"?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38351
    
  23
. . . and I have already made a suggestion, taking the trouble to run your app myself. So don't post the same thing twice.
Ryan Speller
Greenhorn

Joined: Mar 01, 2008
Posts: 9
Ok, I am such a beginner with Java so its really hard for me to know whats like well known as of terms or what my professor is just using in our class, that from what I can tell no one else has really done.

Heres the runtime output I get from running the program:


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')...
name
Enter type of argument value (string, integer, double)...
string
Enter a string value
john connor
Enter URL argument name (or 'done')...
book
Enter type of argument value (string, integer, double)...
integer
Enter an integer value
666222
Enter URL argument name (or 'done')...
done
URL with appended arguments is:
http://www.amazon.comid=123name=john+connorbook=666222

Enter URL site (or 'exit')...


Basically with the URL it is coming out like this:
http://www.amazon.comid=123name=john+connorbook=666222
When I need it to look like this:
http://www.amazon.com?id=123&name=john+connor&book=666222

and of course this all depends on how the URL is entered into the program.

I posted the class I have up above and that is running within the source code that my professor gave me here:



So what I am trying to figure out is a way to put the ? after the .com, like www.amazon.com?id=123, and then put the & after each proceeding argument, like www.amazon.com?id=123&name=john+connor&book=666222 having the & marks end at the last argument so I dont end up with something like, www.amazon.com?id=123&name=john+connor&book=666222&.

Also I cant just put a ? after the URL, because incase I enter just www.amazon.com without any arguments, it doesnt come out looking like www.amazon.com?, but comes out looking like just normal www.amazon.com.

So I need some sort of code (I am so lost on what are the right words to use to describe what I need) to check where each name=value (ex. id=123) comes after the original URL to decide where to put the ? and & in the URL.

Sorry for the vagueness before, but as you can tell I am very frustrated with this and the professor for this class is not very helpful at all.

THANK YOU SO MUCH!!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

So I need some sort of code (I am so lost on what are the right words to use to describe what I need) to check where each name=value (ex. id=123) comes after the original URL to decide where to put the ? and & in the URL.

Sorry for the vagueness before, but as you can tell I am very frustrated with this and the professor for this class is not very helpful at all.


Hate to break the news to you, but I think your professor is not being vague at all -- he pretty much came really close to giving you the answer !!

I think you are coming at the problem the wrong way. You want to fix the broken URL by inserting the "?" and "&" in to the URL. In fact, you should just fix the addArgument() method... when the URL is being generated in the first place.

The addArgument() method should keep track of the times that it is being called (ie. the number of arguments). The first time, it will need to append the "?" first, and sequential times, it will need to append the "&" first. This is also why your profession suggested using an instance variable, as using local variables won't keep state between calls.

Henry
Ryan Speller
Greenhorn

Joined: Mar 01, 2008
Posts: 9
eh... Yeah I figured it was something like that. To put it easy, I am awful at programming, this is a required course and I have just had wayyyyy too tough of a time with it.
I really hate to be asking so much because you have now clarified a better portion of it.
But could you possibly show me what it would actually look like?
You can just give me an example not with my code if this has some sort of moral impact on how you go about helping people.

THANK YOU SO MUCH THOUGH!
Ryan Speller
Greenhorn

Joined: Mar 01, 2008
Posts: 9
Could anyone give me a hand? Basically this assignment was a due awhile ago, and I am just terrible at java programming and seriously dont know how to use an instance variable to count the number of addArguments in my class to decide where to put ? and &. I would really appreciate any type of example someone could give me of what this needs to look like.

Thank you again
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You already have an example. In the code you originally posted, the variable "mUrl" is an instance variable. Notice that it isn't inside a method or a constructor; if it were, then it would be a local variable.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

Not only that, to add a counter and an if condition to the addArgument() method, to add the punctuations, is what? ... 2 lines... 3 lines at most. It is certainly much much easier than what you have done so far.

You keep saying you are "terrible at java programming" -- and don't seem to want to attempt it without help.... but quite frankly, this change is nothing compared to what you have done so far. What is it about this last one percent of your homework assignment that is different from the previous 99%, that you don't feel comfortable that you can do it?

Give it a try. I have pretty much explained everything in english "pseudo code".

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Counter Instance Variable