Results 1 to 2 of 2
  1. #1
    Jeremy5 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Somtimes HTML is printed in my code.

    So I wrote a program to get the current time from this website: Current local time in U.S.A. – Connecticut – Stamford.
    Now here's whats weird about my program, sometimes its prints what I want:

    Steaming Live From: Current local time in U.S.A. – Connecticut – Stamford
    5:08:14

    And other times it adds some HTML to the time:

    Steaming Live From: Current local time in U.S.A. – Connecticut – Stamford
    5:09:08 PM</strong> <strong class

    I'm not sure what's going on. All help is appreciated.

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class Internet {
    	public static void main(String[] args) throws Exception {
    		int i = 1;
    		String website = "http://www.timeanddate.com/worldclock/city.html?n=897";
    		URL yahoo = new URL(website);
    		BufferedReader in = new BufferedReader(
    		new InputStreamReader(yahoo.openStream()));
    
    		String inputLine;
    
    		while ((inputLine = in.readLine()) != null) {
    			i = i + 1;
    				if(i == 58) { //Line 58 has the time
    					//System.out.println("Line number" + i + ": " + inputLine);
    					System.out.println("Steaming Live From: " + website);
    					System.out.println(inputLine.substring(1395,inputLine.length()-15446)); //After character 1395 is the time
    				}
    		}
    		in.close();
    		
    	}
    }
    Thanks!

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Somtimes HTML is printed in my code.

    It probably has something to do with how you're grabbing the time. Depending on the length of the line you've read from html isn't too reliable, it'd be a good idea to use regular expressions to pull the exact line that you need out from the stream.

Similar Threads

  1. read html code of web page
    By asheeshiit in forum Advanced Java
    Replies: 9
    Last Post: 01-07-2014, 09:31 PM
  2. I need some java / html code written
    By DeDanan in forum New To Java
    Replies: 2
    Last Post: 03-01-2011, 09:36 AM
  3. Is this the right way of writing html code in java??
    By raju.i in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2010, 08:05 AM
  4. [SOLVED] retrieve the html code of any web page
    By Omarero in forum New To Java
    Replies: 3
    Last Post: 02-18-2009, 10:20 PM
  5. Verify if a file as been, for sure, printed
    By pjmorce in forum Advanced Java
    Replies: 0
    Last Post: 11-20-2008, 10:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •