Running a Java program with input from a file?

Does test. Dat contain the "y" confirmations too? When you pipe in a file for stdin the content of that file must be in legal format as if it was typed in manually.

Up vote 0 down vote favorite share g+ share fb share tw.

I am writing a program that reads the input from a file and then prints it to the screen. When I run it without taking the input from the file, it works perfectly fine. However, every time I try to run it from the file it gives me an "Exception in thread "main" java.util.

NoSuchElementException: No line found at" error that occurs every place the input is suppose to be read. I have no idea what is going on. This program is suppose to take the input from the user, create a Photo object, and then print the information to the screen.

Everything runs fine when I am entering the information manually but when I try to use java PhotoTest NextLine(Scanner. Java:1516) at PhotoTest. ReadPhoto(PhotoTest.

Java:31) at PhotoTest. Main(PhotoTest. Java:74) My code that has input: private static Photo readPhoto(Scanner scanner) throws ParseException { Date dateTaken; Scanner scan = new Scanner(System.

In); String subject = scan.nextLine(); subject = subject.trim(); String location = scan.nextLine(); location = location.trim(); String date = scan.nextLine(); date = date.trim(); if (date. Equals("")){ //if the date is empty it is set to null dateTaken = null; } else { //if a date is entered, it is then parsed DateFormat df = DateFormat. GetDateInstance(DateFormat.

SHORT); dateTaken = df. Parse(date); } String file = scan.nextLine(); file = file.trim(); File photoFile = new File(file); //creates a Photo object from the information entered Photo Photo = new Photo(subject, location, dateTaken, photoFile); return Photo; } public static void main(String args) throws ParseException { boolean endprogram = false; Scanner scan = new Scanner(System. In); //creates a loop so that the user may enter as many photos as they wish while (!endprogram) { System.out.

Println("Would you like to enter a photo (y/n)? "); //if the input is anything other than y, the program ends if(!scan.next(). EqualsIgnoreCase("y")) { endprogram = true; } else { System.out.

Println(readPhoto(scan)); } } } java file user-input java-util-scanner link|improve this question edited May 15 '11 at 12:53skaffman114k8135227 asked Mar 13 '10 at 0:02Kat1359 100% accept rate.

It won't even compile if you don't return a Photo on the readPhoto method, also post the code that actually reads the file would help – marcosbeirigo Mar 13 '10 at 0:58 1 @Katy: it's impossible to help you at this point. You need to be more clear of what your program is supposed to do. When presenting code, also make sure that it captures the essence of the question.

Your current code has MANY problems that aren't even related to the question. – polygenelubricants Mar 13 '10 at 1:04.

When you pipe in a file for stdin, the content of that file must be in legal format as if it was typed in manually. Also, you are creating another Scanner instance for stdin even though one is already passed to readPhoto. Are you sure you need to do this?

In your file you need a carriage return in the last line. That would be the equivalent to what you are typing manually. Note that when you are typing, in the last line you press enter.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions