# PrintWriter questions

• 10-28-2013, 05:26 PM
shin777
PrintWriter questions
Code:

```import java.util.Scanner; //import scanner statement import java.io.*; public class DistanceTraveled2 {         public static void main(String [] args) throws IOException         {                 Scanner keyboard = new Scanner (System.in); //scanner object       String fileName;                 int speed;                                  //speed of vehicle       int hour;                                  //hour vehicle traveled       int maxHour;                                //max hour vehicle traveled       int distance;                              //distance vehicle traveled             //Prompt vehicle speed       System.out.print("What is the speed of vehicle in miles per hour? ");        speed = keyboard.nextInt();             while (speed < 0) //while statement if prompt speed number is less than 0       {          System.out.print("Invalid input - Please enter positive number: ");         speed = keyboard.nextInt();       }             //Prompt hours vehicle traveled       System.out.print("What is the number of hours it has traveled? ");        maxHour = keyboard.nextInt();             while (maxHour< 0) //while statement if prompt max hour is less than 0       {          System.out.print("Invalid input - Please enter positive number: ");         maxHour = keyboard.nextInt();       }             //Get the file name       System.out.print("Enter the file name: ");       fileName = keyboard.nextLine();             //Open the file       PrintWriter outputFile = new PrintWriter(fileName);             //Print out the result set up       outputFile.println("");       outputFile.println("Hour            Distance Traveled");       outputFile.println("---------------------------------");                   for  (hour = 1; hour <= maxHour; hour++) //for statement to show asked result       {         distance = speed*hour;         outputFile.println(hour + "\t\t\t" + distance); //Print calculated distance for each hour       }       outputFile.close();   } }```
Trying to use Printwriter to print out result but I must have made mistake somewhere. It compiles but doesn't I get errors after I input hours and speed. Expected result suppose to print out

Hour Distance Traveled
---------------------------------
1 30
2 60
3 90

like that if i prompt 3 for hours and 30 for speed. it should prompt user again if i put negative value for speed or hours. It works and print out correct result as above if I don't use PrintWriter but when I use PrintWriter I get
bunch of error messages saying

Enter the file name: Exception in thread "main" java.io.FileNotFoundException:
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:212)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:104)
at java.io.PrintWriter.<init>(PrintWriter.java:184)
at DistanceTraveled2.main(DistanceTraveled2.java:49)

what am I doing wrong here? also, I tried to use Printwriter demo code from book but notepad don't pop out even if it compiles and runs perfect. I am using latest Jgraps.
• 10-28-2013, 05:40 PM
Norm
Re: PrintWriter questions
What was the value of filename when the PrintWriter constructor was executed?

Add a println() that prints out its value. Be sure to delimit the String so you see all of it:
System.out.println("fn="+ thename +"<");
• 10-28-2013, 05:41 PM
jim829
Re: PrintWriter questions
After you are done prompting for hours, there is still a line terminator character in the input buffer. It is throwing the prompt for your filename out of sync. You need to simply put in a keyboard.nextLine() call after you prompt for hours to "absorb" the lingering terminator.

Regards,
Jim
• 10-28-2013, 06:03 PM
shin777
Re: PrintWriter questions
Quote:

Originally Posted by jim829
After you are done prompting for hours, there is still a line terminator character in the input buffer. It is throwing the prompt for your filename out of sync. You need to simply put in a keyboard.nextLine() call after you prompt for hours to "absorb" the lingering terminator.

Regards,
Jim

thx. i threw in keyboard.nextLine(); right above get the file name and now it runs without error.