Results 1 to 5 of 5
  1. #1
    xx__rose is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default Exception in thread "main" java.util.NoSuchElementException

    Java Code:
    public class Animal
    {
       protected int id;
       protected String type;
       protected double mass;
     
        //------------------------------------------------------------------------
        //  Sets up an animal with the specified ID number, type and weight.
        //------------------------------------------------------------------------
     
       public Animal (int animalID, String animalType, double weight)
       {
          id = animalID;
          type = animalType;
          mass = weight;
       }
     
       //------------------------------------------------------------------------
       //  Returns information about this animal as a string.
       //------------------------------------------------------------------------
     
       public String toString()
       {
          String description = "ID: " + id + "\n";
     
          description += "Type: " + type + "\n";
          description += "Weight: " + mass + "\n"; 
     
          return description;  
       }
    }
    Java Code:
    public class Pet extends Animal
    {
       private String title;
       private String own;
     
       //------------------------------------------------------------------------
       //  Sets up a pet using the specified information.
       //------------------------------------------------------------------------
     
       public Pet (int animalID, String animalType, double weight, String name, String owner)
       {
          super (animalID, animalType, weight);
     
          title = name;
          own = owner;
       }
     
       //------------------------------------------------------------------------
       //  Returns information about this pet as a string.
       //------------------------------------------------------------------------
     
       public String toString()
       {
          String description = super.toString();
     
          description += "Name: " + title + "\n";
          description += "Owner: " + own + "\n"; 
     
          return description;  
       }
    }
    Java Code:
    public class ZooAnimal extends Animal
    {
       private int cage;
       private String train;
     
       //------------------------------------------------------------------------
       //  Sets up a zoo animal using the specified information.
       //------------------------------------------------------------------------
     
       public ZooAnimal (int animalID, String animalType, double weight, int cageNumber, String trainer)
       {
          super (animalID, animalType, weight);
     
          cage = cageNumber;
          train = trainer;
       }
     
       //------------------------------------------------------------------------
       //  Returns information about this zoo animal as a string.
       //------------------------------------------------------------------------
     
       public String toString()
       {
          String description = super.toString();
     
          description += "Cage: " + cage + "\n";
          description += "Trainer: " + train + "\n";
     
          return description;
       }
    }
    Java Code:
    import java.util.Scanner;
    import java.util.StringTokenizer;
    import java.io.*;
    
    public class FileRead
    {
    
       public static void main (String[] args) throws FileNotFoundException
       {
          Scanner scan = new Scanner(new File("animal.txt"));
          String animalType = null, name = null, trainer = null, owner = null, lineIn = null;
          int animalID = 0, cageNumber = 0;
          double weight = 0.0;
          StringTokenizer st = null;
          Animal animal = null;
          Pet pet = null;
          ZooAnimal zooAnimal = null;
    
          while (scan.hasNextLine())
          {
             lineIn = scan.nextLine();
             st = new StringTokenizer(lineIn, ",");
             animalID = Integer.parseInt(st.nextToken());
    	 name = st.nextToken();
    
             if (!st.hasMoreTokens())
             {
                animal = new Animal (animalID, animalType, weight);
                System.out.println(animal);
             }
             else
             {
                name = st.nextToken();
                owner = st.nextToken();
                pet = new Pet(animalID, animalType, weight, name, owner);
                System.out.println(pet);
             }
             if (!st.hasMoreTokens())
             {
    	    cageNumber = Integer.parseInt(st.nextToken());
    	    trainer = st.nextToken();
    	    zooAnimal = new ZooAnimal(animalID, animalType, weight, cageNumber, trainer);
    	    System.out.println(zooAnimal);
                System.out.println();
             }
          }
       }
    }
    The following code segment is animal.txt

    Java Code:
    3000,Monkey,38.6
    7999,Ape,65.2,
    8000,Dog,13.4,Thomas,George,
    5252,Giraffe,130.3,103,Samuel,
    Here is my desired output:

    ID: 3000
    Type: Monkey
    Weight: 38.6

    ID: 7999
    Type: Ape
    Weight: 65.2

    (The above from the animal class)

    ID: 8000
    Type: Dog
    Weight: 13.4
    Name: Thomas
    Owner: George

    (The above from the pet class)

    ID: 5252
    Type: Giraffe
    Weight: 130.3
    Cage Number: 103
    Trainer: Samuel

    (The above from the zoo animal class)

    The error that's throwing me off so far is:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
    at FileRead.main(FileRead.java:42)

    What am I doing wrong that's preventing me from my program printing the output?

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

    Default

    You may wish to use some System.out.println statements to test the state of the program and to see what is going wrong. For instance, one great place to place one is here:

    Java Code:
          while (scan.hasNextLine())
          {
             lineIn = scan.nextLine();
             [color="red"][b]System.out.println("lineIn: " + lineIn);[/b][/color] // add System.out.println here!
             st = new StringTokenizer(lineIn, ",");
             animalID = Integer.parseInt(st.nextToken());
    	 name = st.nextToken();

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

    Default

    You don't grab the weight afer you get the type of animal (which you assign to "name" by the way, not animalType).
    This means the if() is always false, since there is always another token.
    This results in the code grabbing the animals name, and then the owners name...but for the first few animals you only have a weight...so you get your exception.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    your input-file is wrong, i correct it to

    Java Code:
    3000,Monkey,38.6
    7999,Ape,65.2,
    8000,Dog,13.4,Thomas,George,
    5252,Giraffe,130.3,Girafy,Samuel,

    so that the name for Giraffe is Girafy and the trainer is Samuel. and the FileRead contained few errors. when you readed the second parameter in the file as name and not as type and so on ... but now the class loook like

    Java Code:
    import java.util.Scanner;
    import java.util.StringTokenizer;
    import java.io.*;
    
    public class FileRead
    {
    
       public static void main (String[] args) throws FileNotFoundException
       {
          Scanner scan = new Scanner(new File("D:/Documents and Settings/t120468/temp/animal.txt"));
          String animalType = null, name = null, trainer = null, owner = null, lineIn = null;
          int animalID = 0, cageNumber = 0;
          double weight = 0.0;
          StringTokenizer st = null;
          Animal animal = null;
          Pet pet = null;
          ZooAnimal zooAnimal = null;
    
          while (scan.hasNextLine())
          {
             lineIn = scan.nextLine();
             st = new StringTokenizer(lineIn, ",");
             animalID = Integer.parseInt(st.nextToken());
             animalType = st.nextToken();
             weight = Double.valueOf(st.nextToken());
    
             if (!st.hasMoreTokens())
             {
                animal = new Animal (animalID, animalType, weight);
                System.out.println(animal);
             }
             else
             {
                name = st.nextToken();
                owner = st.nextToken();
                pet = new Pet(animalID, animalType, weight, name, owner);
                System.out.println(pet);
             }
             if (st.hasMoreTokens())
             {
    	    cageNumber = Integer.parseInt(st.nextToken());
    	    trainer = st.nextToken();
    	    zooAnimal = new ZooAnimal(animalID, animalType, weight, cageNumber, trainer);
    	    System.out.println(zooAnimal);
                System.out.println();
             }
          }
       }
    }

    and produce

    Java Code:
    ID: 3000
    Type: Monkey
    Weight: 38.6
    
    ID: 7999
    Type: Ape
    Weight: 65.2
    
    ID: 8000
    Type: Dog
    Weight: 13.4
    Name: Thomas
    Owner: George
    
    ID: 5252
    Type: Giraffe
    Weight: 130.3
    Name: Girafy
    Owner: Samuel

    it's ok?

  5. #5
    xx__rose is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Replies: 6
    Last Post: 07-08-2009, 07:54 AM
  2. Replies: 12
    Last Post: 12-05-2008, 05:37 AM
  3. Exception in thread "main" java.util.NoSuchElementException
    By vileoxidation in forum New To Java
    Replies: 5
    Last Post: 09-17-2008, 07:29 AM
  4. Replies: 4
    Last Post: 06-08-2008, 02:19 PM
  5. Replies: 5
    Last Post: 05-14-2008, 01:43 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
  •