• Post Reply Bookmark Topic Watch Topic
  • New Topic

java program  RSS feed

 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I want to start by saying thank you for taking the time out to review my post as well as any contributions to my program.

Requirements:

I. i need to accept a string as an argument
II. prints out the first link it find
III. returns an integer that is the index of the link End or -1 if no links are found.

an if statement if it does not find "href" in the string it will return the integer, An if statement to return the -1 before the substring method will keep from getting an index out of bounds error.

The display need to return all the http sites in the string.

I believe i am very close, here is my code:

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chavez,

Is there any particular part of this code that you'd like some help with? And by "help" we mean there's something that you don't quite understand or can't quite figure out and wonder if someone could guide you to enlightenment. We won't, however, complete your work for you or give you a solution.

So, what's your question?
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i believe my program is almost complete because i am doing I,II, and III but i do not know if my if statement is correct or if i am applying the right type of logic. I am really just seeking if i am on the right path to getting the program complete. I know you cannot give answers or solutions but kind of want to know a yes or no answer not a in between or vague answer. I do appreciate your help fully, i have just exhausted all my resources to try and understand this program but does not seem to work properly. When i compile the program it works fine but when i run it then it keeps looping forever and cannot seems to see where it is not breaking because i do have an break statement in my while loop.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few options:
1. Add some strategically placed System.out.println() statements to print out values of variables as your program executes. Then compare the printed values with values that you expect.
2. Walk through the code and using pencil and paper, follow the instructions you have given to the computer and note down the values of the variables as you go. See if the values change as you expect them to.
3. Use an IDE like Eclipse to debug your code, stepping through and inspecting the values of variables to see if they are what you expect them to be.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the loop is not terminating, then the condition for termination is not being satisfied. So figure out what happens with the linkStart variable on each successive iteration. Since linkStart is calculated based on another variable, you'll want to watch the value of that variable as well.
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not a very good debugger and not proficient enough in java to debug a program.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote:I am not a very good debugger and not proficient enough in java to debug a program.

Well, you'll never get better if you don't try it. Give it a shot and share your notes. Or you can go one step at a time and post here; we will be more than happy to help you if we see you've misunderstood something. We like it when folks ShowSomeEffort ←click.
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i am at a loss, i do not know how to debugg with the IDE that i have, i am a fairly new programmer. Well thank you for replying, if you could please delete my post because i do not think i am going to get much help here.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, I'll even start the ball rolling for you:

Starting at line 9, you enter the while-loop for the first time
line 10 - the method hrefSearch() is called with the current htmlCode passed in
line 17 - the method parameter inStr receives the value that got passed in from line 10
line 19 - execution continues with local variable find assigned the value " href="
line 20 - local variable closingBracket is assigned the value ">"
line 23 - index is assigned the value that inStr.indexOf(find) returns, which should be ... ???

Ok, now your turn to debug... keep it going.
 
chavez james
Ranch Hand
Posts: 31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was missing a 1 in my while loop
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to see that you worked it out.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One tip I'd like to offer to you. Choose your names carefully. Names should help convey the ideas in the program so that its intent becomes clear to the reader. Poorly chosen names can obscure what the program is trying to achieve at best. At worst, they can mislead the reader.

Consider this refactored version of your code:

With certain values of the urls, you will still have a bug. (Hint: what happens when the string of urls ends with a series of characters that doesn't have "href=" in it?)
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chavez

Which IDE are you using?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote:Well i am at a loss, i do not know how to debugg with the IDE that i have

The only debugger you need is your brain and some System.out.println (s.o.p) statements.

When I code, I put s.o.p. statements all over the place. I use them to print

When I enter/leave a method
when I enter/leave a loop
which part of a if/else if/else block runs
When some variables change
etc.

I put them in, I take them out, I have them print more or less info...I would estimate that for every line of code that makes it into my final program, i've had (at some point) 2 additional s.o.p statements. They do eventually get taken out before I declare my program done, but they are INVALUABLE in helping me figure out what my program is ACTUALLY doing, vs. what I BELIEVE it is doing.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!