Results 1 to 6 of 6
  1. #1
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Please Help with an Exception that I cannot get rid of

    Java Code:
    import java.util.Scanner;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    
    public class Assignment11c {
    
    	
    	public static void main(String[] args) throws IOException {
    		
    		URL serviceUrl = new URL("http://i5.nyu.edu/~cmk380/cs101/USStates.txt");
    		
    		Scanner input = new Scanner( serviceUrl.openStream() );
    		
    		String [] stateAbbr = null;
    
    		while ( input.hasNext() )
    		{
    			stateAbbr = new String[50];
    			
    			for (int i = 0; i < 50; i++){
    				
    				String data = input.nextLine();
    				String[] splitData = data.split(",");
    				
    				stateAbbr[i] = splitData[1];
    			}
    		
    		}
    		System.out.println(stateAbbr);
    	}
    
    }

    I'm trying to dump the .txt file into an array, but I kept getting this exception error:

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


    It could be that I'm overlooking something, but for now, I cannot for the life of me figure out the problem. If someone can help, that'll be much appreciated!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Please Help with an Exception that I cannot get rid of

    Java Code:
    		while ( input.hasNext() )
    		{
    			for (int i = 0; i < 50; i++){
    				String data = input.nextLine();
    			}
    		}
    You check if there is a nextLine(), then proceed to attempt to read 50 lines.
    What if there were only 49, or 3?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Please Help with an Exception that I cannot get rid of

    Well, if you want to read the next line you should test whether the Scanner has a next line. But are you sure that's what you want to read?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Please Help with an Exception that I cannot get rid of

    It worked fine for this:
    Java Code:
    import java.util.Scanner;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import java.text.DecimalFormat;
    
    public class Assignment11b {
    	
    	public static void main(String[] args) throws IOException {
    	
    	URL serviceUrl = new URL("http://i5.nyu.edu/~cmk380/cs101/ProfessorSalary.txt");
    	
    	Scanner input = new Scanner( serviceUrl.openStream() );
    	
    	String [] profRank = null;
    	double [] profSalary = null;
    	
    	while ( input.hasNext() )
    	{
    		profRank = new String[1000];
    		profSalary = new double [1000];
    		
    		//populate arrays
    		for (int i = 0; i < 1000; i++){
    		
    		String data = input.nextLine();
    		String[] splitData = data.split(" ");
    		
    		
    		profRank [i] = splitData[2]; 
    		profSalary [i] = Double.parseDouble(splitData[3]);
    		
    		}
    	}
    	
    	input.close();
    So why didn't it work for the code posted in the OP?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Please Help with an Exception that I cannot get rid of

    Quote Originally Posted by vx117 View Post
    It worked fine for this:

    So why didn't it work for the code posted in the OP?
    Did you take the time to read Tolls' response?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Please Help with an Exception that I cannot get rid of

    How many lines are there in the txt file?
    I just had a look and there are exactly 1000...so your second code works in this special case.

    Your first one has 51 lines.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  2. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  3. Replies: 1
    Last Post: 02-06-2011, 05:38 AM
  4. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

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