Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: how to read URL

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to read URL

    Hi everybody
    i am a beginner in java programming,
    i want to read from url (which read a temperature from a website and then print it on screen, now subclass is:
    import java.net.*;
    import java.io.*;
    public class NetworkTemperatureSensor extends TemperaturSensor{
        NetworkTemperatureSensor(String sid)
        {
            super(sid);
        }
     
        public Temperature read(){
            Temperature t1 = new Temperature();
     
                    try
    		{       
                            URL url = new URL("http://www.temperatur.nu/termo/boras/temp.txt");
    			URLConnection conn = url.openConnection();
    			InputStream uc = conn.getInputStream();   
    			BufferedReader buffIn = new BufferedReader(new InputStreamReader(uc));
                            String s;   
                        while((s = buffIn.readLine())!= null)
    			{
                                System.out.println(s);   
    			}
                            buffIn.close();
                            t1.setKelvin(Double.parseDouble(s));
     
    		}
    		catch (Exception e)
    		{
    			System.out.println("Exception "+e);
    		}
     
    	return t1;
    }
    }
    into the main method :
     
     
    import java.util.*;
    import java.util.ArrayList;
     
    public class DataLoggerApplikation {
     
     public static void main(String[] args) {
     
         //private Temperature bb;    
         DataLoggerApplikation app = new DataLoggerApplikation();
         Temperature bb = new Temperature();
         app.initiering();
         for(int i = 0; i < 5; i++)
         {
             app.logSensorValues();
         }
         app.skriva();
         bb.equals(bb);
     
     
        }
     
     ArrayList<Sensor> sensorer;
     
     
     public void initiering()
     {
     
        sensorer = new ArrayList();
        sensorer.add(new TemperaturSensor("ts"));
        sensorer.add(new FakeTemperatureSensor("fts"));
        sensorer.add(new FakeTemperatureSensor("fts2"));
     }
     
     
    public void logSensorValues()
    {
     
        Iterator itr = sensorer.iterator();
        Sensor s;
        while(itr.hasNext())
            {
                s = (Sensor) itr.next();
                s.datalogger.add(s.read());
            }
    }
     
    public void skriva()
    {
        Iterator itr = sensorer.iterator();
        Sensor s;
            while(itr.hasNext())
            {
                s = (Sensor)itr.next();
                s.skrivaVarde();
            }
    }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to read URL

    What happens when you compile and execute your program?
    If there are messages, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to read URL

    when i execute program , it shows nothing about url and reading Temp online, because this has not been defined into main method to show something, this program contain many classes but here just come main method and one subclass, i dot know how to take temperature from subclass to main mathod :
    public class NetworkTemperatureSensor extends TemperaturSensor{
    .
    .
    .
    }
    the whole subclass code is in post #1

    should i define any variable in main method?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to read URL

    it shows nothing
    Try debugging the code by adding lots of println statements that print messages to show where the execution flow is going and to show the values of variables as they are changed and used by the program. The printed output will help you understand what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to read URL

    Norm, i think i couldnt explain what i mean exactly. could u please do me favor , Can i zip all the file and send it to you, if u run the code once, u will get what mean.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to read URL

    Post a small simple program here on the forum that compiles, executes and shows the problem.
    Don't post a huge program with lots of code that is not related to the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to read URL

    look post #1 for code, here is result from execution of program:
    Values of Sensor(ts)
    10.0 Kelvin
    10.0 Kelvin
    10.0 Kelvin
    10.0 Kelvin
    10.0 Kelvin
    -------------------------

    Values of Sensor(fts)
    -7.0 Kelvin
    47.0 Kelvin
    -43.0 Kelvin
    -26.0 Kelvin
    -25.0 Kelvin
    -------------------------

    Values of Sensor(fts2)
    -17.0 Kelvin
    -10.0 Kelvin
    -29.0 Kelvin
    28.0 Kelvin
    -27.0 Kelvin
    -------------------------
    as told before, it shows nothing about taking temp from a url ,website. please just show me how to define it into main method to print out online temperature

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to read URL

    Where are you having problems?
    1)Does the code read data from the website OK? Print it out as it is read so you can see it.
    2)Does the code parse the data that was read?
    3)When you have parsed the data, where are you trying to display it?


    look post #1 for code
    The code in post #1 does not compile without errors and can't be executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: how to read URL

    I'm confused about what you want.

    The main method should call the reading method and then return data for processing, surely?
    Your code seems overly complex for what it seems to be designed to do.

    The data is reading successfully, so you need to decide where it goes from there.

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to read URL

    question wants to get value by user and get the value from website, the first part (value by user) works good but problem is how to call that subclass in main method to be readable, i have tried to get temperature online by using NetworkTemperatureSensor subclass and then read the value and display in on screen. but here i want the program to get temperature from the url (website) 5 times and add it to a list which i created a Arraylist<...> sensorer , then i wrote it like this:
    sensorer.add(new NetworkTemperatureSensor("nts"));
    when i execute this code, that just give me this Exception error:
    -2.8
    Exception in thread "main" java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1008)
    at java.lang.Double.parseDouble(Double.java:540)
    at NetworkTemperatureSensor.read(NetworkTemperatureSe nsor.java:24)
    at NetworkTemperatureSensor.read(NetworkTemperatureSe nsor.java:3)
    at DataLoggerApplikation.logSensorValues(DataLoggerAp plikation.java:46)
    at DataLoggerApplikation.main(DataLoggerApplikation.j ava:15)
    Java Result: 1
    this program have 10 classes, but here i just posted 2 class that cause that problem!!!!

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to read URL

    Exception in thread "main" java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1008)
    at java.lang.Double.parseDouble(Double.java:540)
    at NetworkTemperatureSensor.read(NetworkTemperatureSe nsor.java:24)
    There is a variable with a null value when line 24 is executed. Look at line 24 and find the variable with the null value and then backtrack in the code to find out why that variable does not have a valid value.

    posted 2 class that cause that problem!!!!
    You need to change those 2 classes so that they can compile and be executed for testing without requiring any other classes. If the code can not be executed for testing, how can anyone help you find the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Url Image stream? Getting an Image url.
    By turtlemaster in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 30th, 2012, 09:43 AM
  2. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  3. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  4. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  5. Read URL into string - Cookies needed?
    By bhanness in forum Java Networking
    Replies: 0
    Last Post: March 12th, 2010, 05:03 AM