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 9 of 9

Thread: pls.pls.pls.help with this

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default pls.pls.pls.help with this

    /********************************************************************/
    /* Demonstrates the use of Scanner to read file input and parse it  */
    /* using alternative delimiters.                                    */
    /* Output also sent to a file    								   	*/
    /********************************************************************/
    import java.net.*;
    import java.util.Scanner;
    import java.io.*;
     
    public class replaceText
    {
     public static void main (String[]args)throws IOException
     {
     String text, changedText;
     Scanner fileScan, urlScan;
     
     fileScan = new Scanner (new File("input.txt"));
     
     PrintWriter outputFile=new PrintWriter(new FileOutputStream("output.txt"));
     
     //Read and proces each line of the file
     while(fileScan.hasNext());
     {
     text=fileScan.nextLine();
     changedText = text.replaceAll("tutor","thingy");
     outputFile.println(changedText);
     
     }
     fileScan.close();
     outputFile.close();
     }
    }




    im not getting the output........
    Last edited by helloworld922; October 23rd, 2011 at 12:01 AM.

  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: pls.pls.pls.help with this

    what's wrong with your code...write exception with you get on output

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: pls.pls.pls.help with this

    In the brackets where the OP has input.txt and output.txt ,

    Would not have to point to location of the file? i.e. newFile(C:/Documents & Settings/gfdshg/fdsdhfh/Input.txt)


    Its just i was doing a similar program last night and was getting a FileNotFound exception, until i pointed to the exact location of the file.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: pls.pls.pls.help with this

    If it's not a problem, can you show me the text you're trying to edit?
    Also, try the djl1900's suggestion

    Quote Originally Posted by djl1990 View Post
    In the brackets where the OP has input.txt and output.txt ,

    Would not have to point to location of the file? i.e. newFile(C:/Documents & Settings/gfdshg/fdsdhfh/Input.txt)


    Its just i was doing a similar program last night and was getting a FileNotFound exception, until i pointed to the exact location of the file.
    Good observation, that might be the problem. Just a tip... If i remember correctly, if you're writing a file, if the file doesn't exist in the directory, it will be created. When specifiying only the file's name, the directory will be the folder in which your project is located.
    Last edited by Henry518; October 24th, 2011 at 09:31 AM.

  5. #5
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: pls.pls.pls.help with this

    Quote Originally Posted by izzahmed View Post
     
     
     
     
     
     public static void main (String[]args)throws IOException //<--- why? handle your Exception in your code or kill the program
     
     
     PrintWriter outputFile=new PrintWriter(new FileOutputStream("output.txt"));// ensure your text file is there your not creating it, your using it.

    im not getting the output........
    take a look here then see how to use exceptions here

    this should help out then throw in
    System.out.println("Hey Im here");
    to find where your code stops.

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: pls.pls.pls.help with this

    Why are you using a PrintWriter? Use a FileWriter, they are designed for File writing...

    try
    {
      BufferedWriter outWriter = new BufferedWriter(new FileWriter(new File("output.txt")); //Goes to a FILE
     
      //To write
      outWriter.write("I like bananas");
      //To close
      outWriter.flush();
      outWriter.close();
    }catch(IOException e)
    {
      e.printStackTrace();
      return;
    }

  7. #7
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: pls.pls.pls.help with this

    Quote Originally Posted by Tjstretch View Post
    Why are you using a PrintWriter? Use a FileWriter, they are designed for File writing...

    try
    {
      BufferedWriter outWriter = new BufferedWriter(new FileWriter(new File("output.txt")); //Goes to a FILE
     
      //To write
      outWriter.write("I like bananas");
      //To close
      outWriter.flush();
      outWriter.close();
    }catch(IOException e)
    {
      e.printStackTrace();
      return;
    }
    can you explain why you think a PrintWriter is worse than a FileWriter?

    here are the API's...


    FileWriter


    BufferedWriter

    PrintWriter
    Last edited by william; October 24th, 2011 at 12:28 PM. Reason: rephrase

  8. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: pls.pls.pls.help with this

    I thought it was really obvious
    Quote Originally Posted by PrintWriter API
    Methods in this class never throw I/O exceptions. The client may inquire as to whether any errors have occurred by invoking checkError().
    Makes debugging a pain in the ass.

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: pls.pls.pls.help with this

    Quote Originally Posted by william
    here are the API's...
    For what its worth two of those API links are to version 1.4 - which is almost 10 years old. The current release is v1.7, so I encourage those pointing to API's, please point towards a more recent versions (at this time versions 1.6 or 1.7)