Results 1 to 4 of 4
  1. #1

    Default Completely stumped by scanner error in java


    this is a school assignment
    i keep gettng a runtime error that i frankly have no idea how to fix. my output is as follows:

    [Session started at 2009-04-13 07:01:57 -0400.]
    Competitors
    ****************
    ****************

    (Option 1) - Create new Sport Stacker
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1471)
    at Project8Driver.menuSwitch(Project8Driver.java:6
    at Project8Driver.main(Project8Driver.java:54)

    The Debugger has exited with status 1.The Debugger has exited with status 1.

    i have a heiracrchy of classes as follows: competitor (super parent class)-->speller, sportstacker, trackathlete (trackathlete has two subclasses runner and thrower
    the code that generates the error is:


    Code:
    import java.util.Scanner;
    import java.io.BufferedReader;
    import java.io.*;
    
    
    public class Project8Driver
    	{
    		public static void main( String[] args ) throws IOException
    		{
    			/*String fileName = "";			//contains the file name of the input file
    			 boolean event = true;
    			 while (event == true){
    			 //checks if the args[] array contains anything
    			 if (args.length > 0) 
    			 {
    			 String file = args[0];
    			 fileName = file;
    			 event = false;
    			 }
    			 else
    			 {
    			 System.out.println("Please input a file name as a command-line arguement.");
    			 }
    			 }*/
    			
    			//create file scanner
    			Scanner fileScan = null;
    			fileScan = new Scanner(new BufferedReader( new FileReader( "P8input.txt" )) );
    			
    			
    			//scanner for each line
    			//Scanner line = new Scanner( fileScan.nextLine() );
    			
    			//create competitor array
    			Competitor comp[] = new Competitor[20];
    			
    			//initialize input choice
    			int inputChoice = 0;  
    			
    			//go through input as long as inputs are available
    			while( fileScan.hasNext() && inputChoice != 6)
    			{
    				Scanner line = new Scanner( fileScan.nextLine() );
    				inputChoice = line.nextInt();
    				menuSwitch( inputChoice, comp, line);	
    			}
    		}
    		
    		/*********************************************************************************
    		 switch statement that handles competition inputs
    		 *********************************************************************************/
    		public static void menuSwitch( int choice, Competitor comp[], Scanner line)
    		{
    			switch( choice )
    			{
    					//Create new SportStacker
    				case 1:
    					System.out.println("(Option 1) - Create new Sport Stacker");
    					String name = line.nextLine();
    					String birthplace = line.nextLine();
    					String gender = line.nextLine();
    					String age = line.nextLine();
    					int age1 = Integer.parseInt(age);
    					String bestCompFinish = line.nextLine();
    					int bestCompFinish1 = Integer.parseInt(bestCompFinish);
    					String numTimesRecorded = line.nextLine();
    					int numTimesRecorded1 = Integer.parseInt(numTimesRecorded);
    					String ID = line.nextLine();
    					int ID1 = Integer.parseInt(ID);
    					String regionNumber = line.nextLine();
    					int regionNumber1 = Integer.parseInt(regionNumber);
    					String three33 = line.nextLine();
    					boolean event333;
    					if (three33.equalsIgnoreCase("yes"))
    						event333 = true;
    					else
    						event333 = false;
    					String three63 = line.nextLine();
    					boolean event363;
    					if (three63.equalsIgnoreCase("no"))
    						event363 = true;
    					else
    						event363 = false;
    					comp[0] = new SportStacker(name, birthplace, gender, age1, bestCompFinish1, numTimesRecorded1, ID1, event333, event363, regionNumber1 );
    					break;
    					//Create new Speller
    				case 2:
    					System.out.println("(Option 1) - Create new Speller");
    					name = line.nextLine();
    					birthplace = line.nextLine();
    					gender = line.nextLine();
    					age = line.nextLine();
    					age1 = Integer.parseInt(age);
    					bestCompFinish = line.nextLine();
    					bestCompFinish1 = Integer.parseInt(bestCompFinish);
    					regionNumber = line.nextLine();
    					regionNumber1 = Integer.parseInt(regionNumber);
    					String school = line.nextLine();
    					comp[1] = new Speller(name, birthplace, gender, age1, bestCompFinish1, school, regionNumber1);
    					break;
    					//Create new Thrower
    				case 3:
    					System.out.println("(Option 1) - Create new Thrower");
    					name = line.nextLine();
    					birthplace = line.nextLine();
    					gender = line.nextLine();
    					age = line.nextLine();
    					age1 = Integer.parseInt(age);
    					bestCompFinish = line.nextLine();
    					bestCompFinish1 = Integer.parseInt(bestCompFinish);
    					String ageGroup = line.nextLine();
    					String bibNumber = line.nextLine();
    					int bibNumber1 = Integer.parseInt(bibNumber);
    					String bestEvent = line.nextLine();
    					String bestThrow = line.nextLine();
    					double bestThrow1 = Double.parseDouble(bestThrow);
    					comp[2] = new Thrower(name, birthplace, gender, age1, bestCompFinish1, ageGroup, bibNumber1, bestEvent, bestThrow1);
    					break;
    					//Create new Runner
    				case 4:
    					System.out.println("(Option 1) - Create new Runner");
    					name = line.nextLine();
    					birthplace = line.nextLine();
    					gender = line.nextLine();
    					age = line.nextLine();
    					age1 = Integer.parseInt(age);
    					bestCompFinish = line.nextLine();
    					bestCompFinish1 = Integer.parseInt(bestCompFinish);
    					ageGroup = line.nextLine();
    					bibNumber = line.nextLine();
    					bibNumber1 = Integer.parseInt(bibNumber);
    					String distance = line.nextLine();
    					int distance1 = Integer.parseInt(distance);
    					String bestTime = line.nextLine();
    					comp[3] = new Runner(name, birthplace, gender, age1, bestCompFinish1, ageGroup, bibNumber1, distance1, bestTime);
    					break;
    					//Print Competitor Information
    				case 5: 
    					System.out.println("Competitors\n****************");
    					try {
    						for (int i = 0; i < 4; i++){
    							comp[i].String();
    							System.out.println();
    						}
    					}
    					catch (Exception e) {}
    					System.out.println("****************\n");
    					break;
    				case 6:
    					System.out.println("Goodbye");
    					break;
    					//if invalid choice
    				default:
    					System.out.println("\nSorry, invalid choice");
    			}
    		}
    	}

    the input file (with comments) that this comes from looks like this:
    Code:
    5				//print out competitors info
    1				//add sport stacker
    John Smith			//get competitor name
    New York, NY			//get competitor birthplace
    Male				//get competitor gender
    16				//get competitor age
    4				//get competitor best competition finish
    3				//get sport stacker number of times recorded
    638				//get sport stacker ID
    12				//get sport stacker region number
    yes				//get sport stacker T/F for event 3-3-3
    no				//get sport stacker T/F for event 3-6-3
    2				//add speller
    Pat Jones			//get competitor name
    Los Angeles, CA			//get competitor birthplace
    Female				//get competitor gender
    14				//get competitor age
    33				//get competitor best competition finish
    19				//get speller region number
    Washington High School		//get speller school
    3				//add thrower
    Gina Toms			//get competitor name
    Dallas, TX			//get competitor birthplace
    Female				//get competitor gender
    22				//get competitor age
    1				//get competitor best competition finish
    20 - 25				//get thrower age group
    112				//get thrower bib number
    Discus				//get thrower best event
    156.7				//get thrower best distance
    4				//add runner
    Logan James			//get competitor name
    Fort Worth, TX			//get competitor birthplace
    Male				//get competitor gender
    17			       	//get competitor age
    3				//get competitor best competition finish
    15 - 18				//get runner age group
    334				//get runner bib number
    1500				//get runner best distance
    00:04:22			//get runner best time
    5				//print out competitors info
    6				//quit

  2. #2
    After 'googling' the error:

    This error usually happens if you call Scanner#next() without checking hasNext first. Remember you should check this or the equivalent Scanner#hasNextXXX() before every call to Scanner#nextXXX(). You're not doing this. You only check hasNextXXX() at the beginning of the loop.
    I'm not sure kung mao ba jd na ang problem. I try lang.

    Maybe:

    Code:
    if(line.hasNextLine()) {
        String ID = line.nextLine();
    }
    Last edited by doomsweek; 04-13-2009 at 09:34 PM.

  3. #3
    can't understand---

  4. #4
    thanks a bunch
    i'm a little upset i spent all night trying to figure that out when it was a pretty quick fix

  5.    Advertisement

Similar Threads

 
  1. error trapping in java
    By reyarita in forum Programming
    Replies: 7
    Last Post: 12-14-2010, 01:50 PM
  2. About Scanner and BufferReader in java
    By intheb0x in forum Programming
    Replies: 3
    Last Post: 07-08-2010, 02:24 PM
  3. Syntax error in java
    By skeptic_rob in forum Programming
    Replies: 2
    Last Post: 06-14-2010, 07:03 PM
  4. error in loading operating system
    By battleneck in forum Computer Hardware
    Replies: 9
    Last Post: 03-01-2007, 05:51 PM
  5. Pinay gang-raped by 17 men in Kuwait
    By Early_Bird in forum Politics & Current Events
    Replies: 44
    Last Post: 02-28-2007, 09:18 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
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top