Results 1 to 7 of 7
  1. #1
    xpngamer is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default Java Error: NoSuchElementException

    I'm new to java and trying to finish this program. Each time I run it I get a NoSuchElementException. I've included the portion of the code where the error seems to be happening. If anyone knows how to solve this problem that would be great.

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class TackitMJL
    {
    public static void main(String[] args) throws IOException
     {
     	
     PlainMJL plain;
     RobotMJL r1, r2, r3, r4;
     
     plain = new PlainMJL();
     r1 = new RobotMJL();
     r2 = new RobotMJL();
     r3 = new RobotMJL();
     r4 = new RobotMJL();
     
     r1.setRobotStatus();
     r2.setRobotStatus();
     r3.setRobotStatus();
     r4.setRobotStatus();
     
     int flag = 0;
      
     
     String whole_Line;
     
     //Plain Size
     int plain_Size;
    
     //Bomb Locations
     int bomb1_Location_X;
     int bomb1_Location_Y;
     int bomb2_Location_X;
     int bomb2_Location_Y;
     int bomb3_Location_X;
     int bomb3_Location_Y;
     int bomb4_Location_X;
     int bomb4_Location_Y;
     
     //Tackit Location
     int tackit_Location_X;
     int tackit_Location_Y;
     
     Scanner fileScan, Scan;
     
     fileScan = new Scanner (new File("Plain.dat"));
     
     whole_Line = fileScan.nextLine();
     Scan = new Scanner (whole_Line);
     plain_Size = Scan.nextInt();
     
     whole_Line = fileScan.nextLine();
     Scan.useDelimiter(",");
     bomb1_Location_X = Scan.nextInt(); <------ Console says error is here.
     bomb1_Location_Y = Scan.nextInt();
      
     whole_Line = fileScan.nextLine();
     Scan.useDelimiter(",");
     bomb2_Location_X = Scan.nextInt();
     bomb2_Location_Y = Scan.nextInt();
    Not sure if this helps, but the information that is in the plain.dat file is below:

    Java Code:
    100
    5, 10
    15, 25
    6, 99
    17, 3
    12, 12
    Last edited by xpngamer; 03-19-2009 at 06:26 AM.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you forgot to set new Scan.

    Java Code:
        whole_Line = fileScan.nextLine();
        [B]Scan = new Scanner (whole_Line);[/B]
        Scan.useDelimiter[B](", "); // "comma and space" [/B]
        bomb1_Location_X = Scan.nextInt(); //<------ Console says error is here.
        bomb1_Location_Y = Scan.nextInt();
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    xpngamer is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    That solved the first error but another popped up for this portion of the code right below it.

    Java Code:
    whole_Line = fileScan.nextLine();
     Scan = new Scanner (whole_Line);
     Scan.useDelimiter(", ");
     bomb3_Location_X = Scan.nextInt();
     bomb3_Location_Y = Scan.nextInt();
     
     whole_Line = fileScan.nextLine();
     Scan = new Scanner (whole_Line);
     Scan.useDelimiter(", ");
     bomb4_Location_X = Scan.nextInt();
     bomb4_Location_Y = Scan.nextInt(); 
     
     whole_Line = fileScan.nextLine();
     Scan = new Scanner (whole_Line);
     Scan.useDelimiter(", ");
     tackit_Location_X = Scan.nextInt();
     tackit_Location_Y = Scan.nextInt(); <----- InputMismatchException Here

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    try printing out the value of whole_Line and Scan before the error occured.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    xpngamer is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    When I print out what its scanning for whole_Line the output is "12, 12" which is what it should be, then after I use the delimiter and get rid of the "," what it scans next is "12" which again is what it should do, but when it goes to scan the next 12 it gives an Input Mismatch Exception.

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    did you forget the space in useDelimiter?
    useDelimiter(", "); // comma and space.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    xpngamer is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 02-27-2009, 05:19 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
  •