Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

URL Data and Format  RSS feed

 
Jo Jack
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my original code:

I am attempting to have the contents of a URL read and then formatted so it isn't a big jumbled mess anymore. I have already asked the user for input this is where some of my problems begin because I do not know how to make sure the URL they are giving has "http://" included within it. I have already tried to use the URLValidator to know avail. Once that's done I would like to format the URL using my format specifiers that I have learned. Could I have some assistance on this?

 
Campbell Ritchie
Marshal
Posts: 55687
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think you will be moved, since that is not a specific Java8 topic. Have you been through the Java Tutorials section? That tells you how to read things. The URL constructor will throw an Exception if you try the wrong format.
If the URL is passed as a String, you can use methods of the String class to find whether it begins with something. You can also search for regular expressions to match URLs and see whether they match your URL name.
 
Campbell Ritchie
Marshal
Posts: 55687
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added code tags (always use them) to your code, and doesn't it look better.
Where does the URLValidator class come from; I can't find it in the standard Java7 API.
 
Jo Jack
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I'm glad to be here. I have not checked out those sections yet. I've just been so stuck on this for awhile now. I'll go check them out.
 
Jo Jack
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Cambell I really have no idea. I have asked others for help and that's what they pointed me to when I attempted to implement it ,it just didn't work.
 
Campbell Ritchie
Marshal
Posts: 55687
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What didn't work? We need more info than that.
Read the example in the tutorial link I posted earlier. Adding a few lines from that to your code printed the contents of Johnson's website successfully. Have you tried that sort of thing?

I am logging off now, myself.
 
Jo Jack
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright thanks for your help.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this is your question, but the easiest way to fix HTML so it isn't a horrible unreadable mess is to use one of the existing HTML prettifiers. JTidy is a Java product which does that for you, so using that would be far more practical than trying to write your own code to do it.
 
Jo Jack
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul I am trying to learn how to use my format specifiers along with printing out the contents of the URL. Just to remember if asked.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh right, you did mention format specifiers. And yes, I agree that learning to use format specifiers shouldn't be confused with cleaning up messy HTML. Carry on then, and if you have more questions don't hesitate to drop in and ask us.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!