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

    Default New to java, need help with program.

    Hello all,

    I am trying to write a program that has several different functions. The first method asks the user to enter the name of a file, which the program then creates and writes the subsequent input form the user to that file. I have successfully completed that portion. However the next part of the program I am trying to write asks the user for the name of a file, attempts to find it and open it and then read the information forwards and backwards. The problem I am running into is in the second method I have called readFile I run into a problem. The program suiccessfully calls the method and prints the first println statement but then I immediately get this message:

    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:107)
    at FileTest.main(FileTest.java:14)

    I know this exception occurs immediately aftyer the prompt for the filename in the readFIle method when the progrma comes to the line that reads: newFile = inputDevice.next();

    However I have no idea how to fix this. I have a felling that the error resides either in that something is left in the input buffer or that somehow the next or nextline function is picking up a whitespace character which causes it to read that nothing is there. I am not sure because I am not that familiar with the next and nextline methods. Any help that anyone could give would be greatly appreciated. Thanks. My code is as follows:


    Java Code:
    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);
    		String fileName = "";
    		istream = System.in;
    		ostream = System.out;
    		
    		//Asks user for filename.
    		try
    		{
    			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());
    				}
    				keyboard.nextLine();	
    			}			
    			catch(IOException e)
    			{
    				System.out.println("Error: " +e.getMessage());
    			}
    		}					
    				
    		//Closes input, output streams.
    		finally
    		{ 
    			try
    			{
    				istream.close();
    				ostream.close();
    				keyboard.close();
    			}
    			
    			catch(IOException e)
    			{
    				System.out.println("File did not close. ");
    			}
    		}	
    	}	
    			
    	public static void readFile()	
    	{
    			InputStream input;
    			PrintStream output;
    			int lastEntry = 0;
    			final int EOF = -1;
    			Scanner inputDevice = new Scanner(System.in);
    			String newFile = "";
    			int pos = 0;
    			output = System.out;
    				
    			//Asks user for filename.
    			System.out.println("Please enter the filename of the file you want to open: ");
    			
    			System.out.println(inputDevice);
    			System.out.println(newFile);
    			
    			newFile = inputDevice.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 = inputDevice.nextLine();
    			}	
    			
    			try
    			{
    				RandomAccessFile inFile = new RandomAccessFile("readFile", "r");
    				{	
    					while(lastEntry != EOF)
    					output.write(lastEntry);
    				}
    				
    				
    				
    				try
    				{
    					for(int count = EOF; count > 0; --count)
    					{
    						inFile.writeChar(0);
    					
    					}
    				}
    					
    				catch(IOException e)
    				{
    					System.out.println("Error: " +e.getMessage());
    				}
    					
    			}
    			catch(IOException e)
    			{
    				System.out.println("Error: " +e.getMessage());
    			}
    
    
    	}
    }
    Last edited by Eranga; 12-12-2011 at 09:47 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: New to java, need help with program.

    In createFile() you assign istream the value System.in and then latter you call istream.close() ie you close System.in.

    In readFile() you say

    Java Code:
    Scanner inputDevice = new Scanner(System.in);
    But System.in has been closed, which is why inputDevice.next() throws the NoSuchElementException.

    [Edit] Interestingly you don't actually use istream for any purpose other than closing it at the end of the method. It might be a good idea to look closely at your code and only declare variables you need and declare them close to where you intend using them. Likewise values shouldn't be assigned to variables unless you intend using them for something. Eg fileName should be declared within the first try block, and should not be assigned the empty string. These things just make the code difficult to follow.
    Last edited by pbrockway2; 12-12-2011 at 10:13 AM.

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 05:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 01:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 06:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •