Results 1 to 3 of 3
  1. #1
    Lish is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Exception error and I can't find the cause

    I'm writing a program for my Java class and I'm getting the following error:

    Java Code:
    java.util.NoSuchElementException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at prog2.main(prog2.java:50)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
    I looked through my program and I can't find a place that would be causing this. The outfile is written, but there is nothing in it. The infile exists, but I'm thinking that the program is getting caught on my try/catch, even though I think I did it properly. Any assistance would be greatly appreciated. My program is as follows:

    Java Code:
    /*
      This is a program that analyzes card holder annual rebates.
    
    */
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    
    import java.util.Scanner;
    
    
    public class prog2 {
        public static void main(String args[]) throws FileNotFoundException
            {
             Scanner infile;      //input file
             PrintWriter outfile; //output file
    
    
              //create a Scanner object for infile using the file intext.txt
              //infile=new Scanner(new File("intext.txt"));
    
             try //checks for input file
             {
               infile = new Scanner (new File ("intext.txt"));
             }
             catch (FileNotFoundException e)
             {
               System.out.println("Program execution ends because the input file cannot be found");
               return;
             }
             
             outfile = new PrintWriter ("outext.txt");
             
             String name; //card holder's name
             Double purch; //annual purchase
             Double rebate; //holder's rebate
             String comment; //comment on holder
             
             outfile.println("CARD MEMBER       PURCHASE   REBATE  COMMENT");
             
             while (infile.hasNextLine())
             {
                   infile.useDelimiter(""); //makes name = 16 characters
                   name="";
                   
                   for (int i = 0; i < 16; i++)
                     name = name + infile.next();
                   
                   infile.reset();//end of reading name
                   
                   purch = infile.nextDouble();//saves the next double as variable purch
                   
                   infile.nextLine();//skip the end of line character
                   
                   if (purch < 0)
                   { 
                     System.out.println ("Cannot have negative purchase total");
                     return;
                   }
                   else if (purch > 0 && purch < 2000)//whole if statement calculates rebate
                     rebate = (purch * .005);
                   else if (purch > 2000 && purch < 5000)
                     rebate = ((purch - 2000) * .01 + 10);
                   else //if (purch > 5000)
                     rebate = ((purch - 5000) * .015 +40);
                   
                   if (rebate > 30.0)//sets comment on card holder
                     comment = "Great";
                   else
                     comment = "Good";
                   
                   outfile.printf("%s,%5.2f,%5.2f,%s",  name,purch,rebate,comment);//prints report to the outfile
                   
                   outfile.println(); //prints end of line character
                   }
             
             infile.close();
             outfile.close();
          
        } //end main();
    
    } //end class lab03

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Your exception is caused when you attempt to read another token from input when there isn't any. It probably happens because you are making mulitple reads inside the while loop. Consider the following:
    Java Code:
    String text = "one two";
    Scanner scan = new Scanner(text);
    while(scan.hasNextLine()) { // true
        System.out.println(scan.next()); //prints one
        System.out.println(scan.next()); //prints two
        System.out.println(scan.next()); // oh oh, error because there is no more input
    }

  3. #3
    Lish is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Okay... I'm not sure how to fix the program I have with that, though. I guess I'm not understanding where the problem is. The loop is just supposed to go through each line and only repeat when there's another line to go through.

    Am I completely misunderstanding what you're trying to tell me? :/

Similar Threads

  1. Find the error in this and why this can't be ?
    By ron2794 in forum New To Java
    Replies: 11
    Last Post: 12-14-2010, 03:23 PM
  2. I need help with an error! Cannot find symbol error!
    By ambria1975 in forum New To Java
    Replies: 2
    Last Post: 07-07-2010, 01:37 AM
  3. error cannot find symbol
    By jcoon3 in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 10:56 PM
  4. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:39 AM
  5. Error: cannot find symbol
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:12 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
  •