i try to print out SAN FRANCISCO and its weather contents when practising, but i can only print the whole page. i need to print ony san francisco and its content making 4 lines altogether.
Code java:
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
// TODO code application logic here
URL url = null;
try {
url = new URL("http://www.nws.noaa.gov/view/prodsByState.php?state=CA&prodtype=state");
} catch (MalformedURLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
URLConnection uc = null;
try {
uc = url.openConnection();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
DataInputStream input = null;
try {
input = new DataInputStream(uc.getInputStream());
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
String inputLine;
String result = null;
while ((inputLine = input.readLine()) != null) {
if(inputLine == " SAN FRANCISCO")
System.out.println(inputLine);
}
input.close();
}
}
Re: i try to print out SAN FRANCISCO and its weather contents when practising, but i can only print the whole page. i need to print ony san francisco and its content making 4 lines altogether.
One problem I see is the use of == to compare Strings. You should use the equals() method for comparing Strings.