Results 1 to 4 of 4
  1. #1
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default PrintWriter questions

    Java 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.
    Last edited by shin777; 10-28-2013 at 06:28 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,781
    Rep Power
    25

    Default 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 +"<");
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: PrintWriter questions

    Quote Originally Posted by jim829 View Post
    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.
    Last edited by shin777; 10-28-2013 at 07:06 PM.

Similar Threads

  1. PrintWriter problems
    By mickylad in forum New To Java
    Replies: 9
    Last Post: 11-13-2011, 04:23 PM
  2. Need help with PrintWriter!
    By PapaEcho in forum New To Java
    Replies: 2
    Last Post: 10-09-2011, 07:51 PM
  3. printwriter
    By sope in forum New To Java
    Replies: 2
    Last Post: 05-09-2011, 08:57 PM
  4. Help with printwriter.
    By Addez in forum New To Java
    Replies: 2
    Last Post: 10-30-2009, 02:58 PM
  5. Using Printwriter (java.io.*)
    By LifeWithJava in forum New To Java
    Replies: 3
    Last Post: 12-25-2008, 09:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •