Results 1 to 7 of 7
  1. #1
    mpthegeneral is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Exclamation NoSuchElementException... help!!

    Hello,
    I'm new to java and I'm attempting to write a simple database using static arrays.
    The problem that i cannot get past is in a method that reads a text file into the array.

    Below is the .txt file that the method is reading from.
    (note: "|" is used as the delimeter and there are not supposed to be lines between each entry in the .txt file.)
    Java Code:
    Bob|Marley|1993@yahoo.com|Monday, Tuesday, Thursday before and after school|Intermediate|CompSci 
    
    John|Doe|jd@yahoo.com|Monday, Thursday after school|Novice|CompSci 
    
    Billy|Mays|bm@yahoo.com|Monday, Tuesday before school|Expert|CompSci 
    
    Fred|Flintstone|fff@yahoo.com|Monday, Tuesday, Thursday before and after school|Newbie|CompSci

    And this is the file reading method:
    Java Code:
    public void read(File f)
        {
        	//clear array
        	members = new TeamMember[100];
        	count = 0;
       	
        	Scanner input = null;
    		try 
    		{
    			input = new Scanner(f);
    			input = input.useDelimiter("|");
    			
    			while(input.hasNext())
    			{
    				String first = input.next();
    				String last = input.next();
    				String email = input.next();
    				String avail = input.next();
    				String skill = input.next();
    				String exp = input.next();
    				
    				if(input.hasNext())
    				{
    					input.nextLine();
    				}
    			
    				TeamMember temp = new TeamMember(first, last, email, avail, skill, exp);
    				addMember(temp);
    				
    				count++;
    			}
    			
    			input.close();
    			
    		} 
    		catch (FileNotFoundException e) 
    		{
    			System.out.println("/n	ERROR: " + e.getMessage());
    		}
        }

    I have not the slightest clue on how to fix this issue. I've been searching around on Google for the past 3 hours.
    Please help me solve this issue, this is due soon.. :(
    Any and all help is greatly appreciated! :D
    Last edited by mpthegeneral; 03-19-2011 at 11:09 PM.

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    What is the exact runtime error message? And what line of your code does it refer to?

    ----------------

    You can get more information about exactly where the problem occurs (both in terms of your code and in terms of the data file) by adding some diagnostic output.

    Java Code:
    while(input.hasNext())
    {
        String first = input.next();
        String last = input.next();
        String email = input.next();
        String avail = input.next();
        String skill = input.next();
        String exp = input.next();
        
        [color=blue]System.out.printf("Read %s %s %s %s %s %s%n", first, last, email, avail, skill, exp);[/color]
    				
        if(input.hasNext())
        {
            input.nextLine();
        }
    			
        TeamMember temp = new TeamMember(first, last, email, avail, skill, exp);
        addMember(temp);
    				
        count++;
    }

  3. #3
    mpthegeneral is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Oh, I'm sorry I forgot that. Here it is:
    Java Code:
    Exception in thread "main" java.util.NoSuchElementException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at TeamDatabase.read(TeamDatabase.java:79)
    	at CSTeam.dispatch(CSTeam.java:142)
    	at CSTeam.main(CSTeam.java:53)
    And its is referring to line 79 which is
    Java Code:
    String email = input.next();
    in the read method.

    EDIT: Also I did the diagnostic output you suggested and received the following:
    Java Code:
    Read Bob | Ma
    Read 
     
     J o h n
    Read 
     
     B i l l
    Read 
     
     F r e d
    What does it mean?
    Last edited by mpthegeneral; 03-19-2011 at 11:52 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Myself, I would use a standard Scanner with the default delimiter to get each line in the file via:

    Java Code:
    while (input.hasNextLine) {
      String line = input.nextLine();
      //...
    }
    and then would split each line using String#split(...) using "\\|" as the regex for splitting. I would use \\ to escape the | character since | has special meaning in regex.

    If you must use Scanner, then I'd use two Scanners, one to read in each line that uses the default delimiter (similar to above) and a second that works on each line obtained. I would still escape the | character the same: "\\|" for my Scanner delimiter.

  5. #5
    mpthegeneral is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you but I am fairly new to this and I don't understand much of what you said. This program pretty much entails all of my current knowledge of Java; however, I did escape the delimeter and the new diagnostic output became this:

    Java Code:
    Read Bob Marley bm@yahoo.com Monday, Tuesday, Thursday before and after school Intermediate CompSci 
     
    Read John Doe jd@yahoo.com Monday, Thursday after school Novice CompSci 
     
    Read Billy Mays bm@yahoo.com Monday, Tuesday before school Expert CompSci 
     
    Read Fred Flintstone fff@yahoo.com Monday, Tuesday, Thursday before and after school Newbie CompSci
    Does this mean it worked?
    *Sorry for all the edits..
    Last edited by mpthegeneral; 03-19-2011 at 11:52 PM.

  6. #6
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Does this mean it worked?

    Hard to say without knowing the code... (did you stick with one scanner and escape the | when you set the delimiter?)

    Anyway, where in the world is John Doe? Somehow or other "<newline>John" got included as part of the Bob Marley data. And then the rest of John Doe's data was lost.

    I agree with foobarable. You are doing two things with the text in the file: splitting on newlines, then splitting on the pipe. To elaborate on his code:

    Java Code:
    import java.io.StringReader;
    import java.util.Scanner;
    
    public class RecordReader {
    
        public static void main(String[] args) {
                // we can imitate reading from a file
            String data = "red|3|stop\ngreen|5|go\nblue|4|no value";
            Scanner input = new Scanner(new StringReader(data));
    		
            while(input.hasNextLine()) {
                String line = input.nextLine();
                String[] fields = line.split("\\|");
    			
                System.out.printf("Data read: %s %s %s%n", fields[0], fields[1], fields[2]);
                // now do whatever you like with the fields
            }
        }
    }

  7. #7
    mpthegeneral is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    ... (did you stick with one scanner and escape the | when you set the delimiter?)
    Yes I did and it worked.

    Anyway, where in the world is John Doe?
    That was just an issue with my .txt file which I have fixed.

    I still don't understand much of the code you posted but thank you. I think my problem is now resolved! :D

    Much thanks Fubarable and pbrockway2!! :)

Similar Threads

  1. Newbie JCreator NoSuchElementException Help!
    By Sly Cooper in forum JCreator
    Replies: 2
    Last Post: 12-18-2012, 06:44 PM
  2. Replies: 6
    Last Post: 03-11-2011, 04:53 AM
  3. nosuchelementexception
    By nicaskdjf in forum New To Java
    Replies: 1
    Last Post: 02-24-2011, 06:14 AM
  4. Java Error: NoSuchElementException
    By xpngamer in forum New To Java
    Replies: 6
    Last Post: 03-19-2009, 08:37 PM
  5. Replies: 5
    Last Post: 02-27-2009, 06:19 AM

Tags for this Thread

Posting Permissions

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