Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Not getting an output file

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Not getting an output file

    For some reason I am not getting an output file created in eclipse when I run this program. I get this error when I run it.
    Exception in thread "main" java.util.InputMismatchException blah blah and more blah

    example of my input file is

    Michael		15
    Jessica		16
    Christopher	        20
    Ashley		18
    Matthew		25
    Emily		        22

    I dont have an output file I thought that when you open a file for writing it will create the file if it does not exist. Idk what Is all going wrong any help would be appreciated.


     import java.io.File;
     import java.io.FileNotFoundException;
     import java.io.PrintWriter;
     import java.util.Scanner;
     
     public class NameAgeReverse{
     
    	public static void main(String[] args) throws FileNotFoundException 
    	{
     
    		Scanner enter = new Scanner(System.in);
    		String inputFile, outputFile;
    		File input;
    		PrintWriter output;
     
    		System.out.println("Input file: ");
    		inputFile= enter.nextLine();
    		System.out.println("Output file: ");
    		outputFile= enter.nextLine();
     
    		input = new File(inputFile);
    		Scanner in = new  Scanner(input);
    		output = new PrintWriter(outputFile);
     
    		while(in.hasNextLine()){
    			String name = in.nextLine();
    			int age= in.nextInt();
    			output.println(name + age);			
    		}
     
    		in.close();
    		output.close();		
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Not getting an output file

    java.util.InputMismatchException
    You need to copy the full text of the error message and post it here. It tells us where the error happened.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Not getting an output file

    blah blah and more blah
    That's the useful part.

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Not getting an output file

    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at NameAgeReverse.main(NameAgeReverse.java:28)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Not getting an output file

    The Scanner class is complaining that the data being read by the nextInt() method at line 28 is not numeric.

    There are problems using the Scanner class's nextLine() and nextInt() methods together. Here's some info on what happens and what to do: Java Scanner not working, difference between next() and nextLine() | Rommel Rico

    Also some of my old notes:
    Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

    For example if you enter: A word to the wise <PRESS ENTER>
    and use next() only "A" is read, and "word to the wise" is left in the buffer.
    Your next attempt to get something from Scanner will be to get "word".
    nextInt() will fail here.
    To clear the buffer, use the nextLine() method.
    To test if the next token is an int, use the hasNextInt() method.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    camel-man (October 6th, 2013)

Similar Threads

  1. Replies: 19
    Last Post: March 15th, 2013, 03:11 PM
  2. [SOLVED] File input and file output
    By maple1100 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 26th, 2012, 10:11 PM
  3. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  4. File Output
    By Nuggets in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 3rd, 2012, 08:55 AM
  5. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 06:26 PM