0

I'm trying to input some addresses from an online text file, but there are errors I don't understand when running the code.

The errors are:

Exception in thread "main" java.util.NoSuchElementException: No line found  
      at java.util.Scanner.nextLine(Unknown Source)  
    at Main.main(Main.java:20)  

What am doing wrong?

I opened the text of the url with the browser and it opens right(Problem is not with the connection or the link)
I'm Using Windows command prompt

import java.net.URL;
import java.util.Scanner;

class Address
{
    String street;
    String city;
    String state;
    int zip;
}

public class WebAddresses
{
    public static void main(String[] args) throws Exception
    {
        URL addys = new URL("http://cs.leanderisd.org/txt/fake-addresses.txt");
        Scanner fin = new Scanner( addys.openStream() );

        Address uno = new Address();
        uno.street = fin.nextLine();  //line 20
        uno.city   = fin.nextLine();
        uno.state  = fin.next();
        uno.zip    = fin.nextInt();
        fin.skip("\n");

        Address dos = new Address();
        dos.street = fin.nextLine();
        dos.city   = fin.nextLine();
        dos.state  = fin.next();
        dos.zip    = fin.nextInt();
        fin.skip("\n");

        Address tre = new Address();
        tre.street = fin.nextLine();
        tre.city   = fin.nextLine();
        tre.state  = fin.next();
        tre.zip    = fin.nextInt();
        fin.skip("\n");

        fin.close();

        System.out.println(uno.street + ", " + uno.city + ", " + uno.state + "  " + uno.zip);
        System.out.println(dos.street + ", " + dos.city + ", " + dos.state + "  " + dos.zip);
        System.out.println(tre.street + ", " + tre.city + ", " + tre.state + "  " + tre.zip);
    }
}
farshad
  • 764
  • 10
  • 25

3 Answers3

3

That URL returns a 302 "Object Moved" response redirecting you to a secure protocol; note the https in the response's Location header:

curl -v http://cs.leanderisd.org/txt/fake-addresses.txt

*   Trying 204.57.104.225...
* Connected to cs.leanderisd.org (204.57.104.225) port 80 (#0)
> GET /txt/fake-addresses.txt HTTP/1.1
> Host: cs.leanderisd.org
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 302 Object Moved
< Date: Thu, 07 Apr 2016 20:13:01 GMT
< Connection: Keep-Alive
< Content-Length: 0
< Location: https://cs.leanderisd.org/txt/fake-addresses.txt
< 
* Excess found in a non pipelined read: excess = 1 url = /txt/fake-addresses.txt (zero-length body)
* Connection #0 to host cs.leanderisd.org left intact

You'll have to follow the redirect, as explained here: How to get redirected URL and content using HttpURLConnection

Or just use the secure URL to start with:

https://cs.leanderisd.org/txt/fake-addresses.txt
Community
  • 1
  • 1
dnault
  • 8,340
  • 1
  • 34
  • 53
1

There is no next line. This may be because the stream is empty or because its contents are all on one line you can check if there is a next line using fin.hasNextLine()

joe pelletier
  • 390
  • 2
  • 12
1

The problem is not in your code, but in the server. First try shortening your example, like this:

public static void main(String[] args) throws Exception {
  URL addys = new URL("http://purple.com");
  Scanner fin = new Scanner( addys.openStream() );
  System.out.println(fin.nextLine());
}

Note that I changed the url from your example to http://purple.com. This works great! Switch it back to http://cs.leanderisd.org/txt/fake-addresses.txt and it fails. It appears the server is configured in some way that is preventing access by your code.

Eric Grunzke
  • 1,487
  • 15
  • 21