Results 1 to 6 of 6
  1. #1
    WhiteRaven6 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Help with NoSuchElementException, relates to Scanner class

    Hi all,

    I am new to java and trying to wirte a program with 2 methods. Everything works fine until the second method called readFile. I ask the user to input the name of the file they want to open and immediately after that the program encounters a runtime error. I know the error occurs at the line that reads newFile = keyboard.next(); I believe that this has something to do with either some whitespace character being read by next() imediately or that the scanner object keyboard still is open or has data in it that is cuasing this error. I have tried everything from adding extra next(0 statements to closing the keyboard object in the previous method but nothing seems to work. This is for a class and the instructor is not responding to my requests for help so if anyone can point this out for me it would be greatly appreciated. Thanks. Coe follows:


    import java.util.*;
    import java.io.*;

    public class FileTest
    {
    public static void main(String[] args)
    {
    createFile();
    readFile();
    }

    public static void createFile()
    {

    //Variables
    InputStream istream;
    PrintStream ostream;
    int lastEntry = 0;
    final int EOF = -1;
    Scanner keyboard = new Scanner(System.in);
    istream = System.in;
    ostream = System.out;

    //Asks user for filename.
    try
    {
    String fileName;
    System.out.println("Please enter the filename of the file you want to open: ");
    fileName = keyboard.next();

    //Creates specified file.
    File currentFile = new File(fileName);

    //Checks if file already exists.
    while(currentFile.exists())
    {
    System.out.println(fileName + " already exists");
    System.out.println("Error: To prevent tis file from being overwritten please enter another file name");
    fileName = keyboard.next();
    }

    //Asks user for information they want stored in file.
    try
    {
    ostream = new PrintStream(fileName);
    System.out.println("Please enter what you would like ot put in the file and press Ctrl+Z when finished: ");

    //Writes information to file.
    try
    {
    while((lastEntry = istream.read()) != EOF)
    ostream.write(lastEntry);

    }
    catch(Exception e)
    {
    System.out.println("Error: " +e.getMessage());
    }

    }
    catch(IOException e)
    {
    System.out.println("Error: " +e.getMessage());
    }
    }
    finally
    {
    }





    }

    public static void readFile()
    {
    InputStream input;
    PrintStream output;

    int lastEntry = 0;
    final int EOF = -1;
    output = System.out;

    Scanner keyboard = new Scanner(System.in);
    int pos = 0;
    input = System.in;

    //Asks user for filename.
    String newFile;
    System.out.println("Please enter the filename of the file you want to open: ");
    newFile = keyboard.next();

    File currentFile = new File(newFile);

    while(!currentFile.exists())
    {
    System.out.println("THis file does not exist");
    System.out.println("Please enter a valid fileneame");
    newFile = keyboard.nextLine();
    }
    try
    {
    input = new FileInputStream(currentFile);
    }
    catch(IOException e)
    {
    System.out.println("file not found");
    try
    {

    {
    while((lastEntry = input.read()) != EOF)
    output.write(lastEntry);
    }



    try
    {
    for(int count = EOF; count > 0; --count)
    {


    }
    }

    catch(Exception f)
    {
    System.out.println("Error: " +e.getMessage());
    }

    }
    catch(IOException g)
    {
    System.out.println("Error: " +e.getMessage());
    }


    }
    }
    }

  2. #2
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Help with NoSuchElementException, relates to Scanner class

    I'm pretty buzy right now and the code seems moderatly confusing in the way that it's laid out

    one thing to definitly look for is useing the nextLine() or next() ( i forget which one wont work ) for both ints and for strings, it will only allows one data type per scanner if you use int and string so it may require two scanners

    ** didnt take the time to notice if you used the scanner for both a int and string just wanted to let you know in case its the problem
    Last edited by kevinn205; 12-12-2011 at 10:07 PM.

  3. #3
    WhiteRaven6 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Help with NoSuchElementException, relates to Scanner class

    No I know what you are referring to in relation to the nextline() method so that you have to add an extra nextLine statement in if going from an int to line etc. but thats not the issue. I am asking for two separate strings whcih represent file names. 1 each for each method. I am pretty sure that something is left over in my scanner object keyboard from the first method createFile so when I try to use keyboard.next() again it interferes I am just not sure how to fix it.

  4. #4
    WhiteRaven6 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Help with NoSuchElementException, relates to Scanner class

    Ok I am pretty sure that the problem has to do with the fact that I am trying to use the scanner class to accept input from the user twice, once in each method. Thats because If I remove the createfile method the readfile method works as it should. However I am still not sure what exactly is going on and how to fix it.

  5. #5
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Help with NoSuchElementException, relates to Scanner class

    Sorry I thought i responded simplest solution create another scanner

    there is not limit to the number of scanners you can use

    Scanner keyboard2 = new Scanner(System.in);

  6. #6
    WhiteRaven6 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Help with NoSuchElementException, relates to Scanner class

    Nope that doesn't work either. I modified the code adn compiled but still get the exact same exception message. I also added a print statement to see what was in the scanner object right before the statement that is causing the problem executes.

    The print statement shows the value of keyboard2 as:

    java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]

    Here is the error message I still receive:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at FileTest.readFile(FileTest.java:91)
    at FileTest.main(FileTest.java:13)

    and Here is the code:

    //ETROY Advanced Programming I CS-2265-XTIA 11/T2 (Haycock)
    // Week 8 Programming Assignment
    // Written by Ryan Pigmon

    import java.util.*;
    import java.io.*;

    public class FileTest
    {
    public static void main(String[] args)
    {
    createFile();
    readFile();
    }

    public static void createFile()
    {

    //Variables
    InputStream istream;
    PrintStream ostream;
    int lastEntry = 0;
    final int EOF = -1;
    Scanner keyboard = new Scanner(System.in);
    istream = System.in;
    ostream = System.out;

    //Asks user for filename.
    try
    {
    String fileName;
    System.out.println("Please enter the filename of the file you want to open: ");
    fileName = keyboard.nextLine();

    //Creates specified file.
    File currentFile = new File(fileName);

    //Checks if file already exists.
    while(currentFile.exists())
    {
    System.out.println(fileName + " already exists");
    System.out.println("Error: To prevent tis file from being overwritten please enter another file name");
    fileName = keyboard.nextLine();
    }

    //Asks user for information they want stored in file.
    try
    {
    ostream = new PrintStream(fileName);
    System.out.println("Please enter what you would like ot put in the file and press Ctrl+Z when finished: ");

    //Writes information to file.
    try
    {
    while((lastEntry = istream.read()) != EOF)
    ostream.write(lastEntry);

    }
    catch(Exception e)
    {
    System.out.println("Error: " +e.getMessage());
    }

    }
    catch(IOException e)
    {
    System.out.println("Error: " +e.getMessage());
    }
    }

    finally
    {
    }

    }

    public static void readFile()
    {
    InputStream input;
    PrintStream output;
    output = System.out;
    int lastEntry = 0;
    final int EOF = -1;
    Scanner keyboard2 = new Scanner(System.in);

    //Asks user for filename.
    String newFile;
    System.out.println("Please enter the filename of the file you want to open: ");

    System.out.println(keyboard2);
    newFile = keyboard2.next();

    }

Similar Threads

  1. Using the Scanner Class with jdb
    By flubbernugget in forum New To Java
    Replies: 4
    Last Post: 07-05-2011, 03:17 AM
  2. Help with Scanner Class
    By VisionIncision in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 03:53 PM
  3. Scanner Class...
    By TheRocket in forum New To Java
    Replies: 5
    Last Post: 12-05-2008, 10:48 AM
  4. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 08:01 AM
  5. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 07:55 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
  •