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

Thread: Help here!!

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

    Default Help here!!

    what's wrong in this code:

    import java.io.*;

    public class WriteFile{

    public static void main(String[] args) throws IOException{
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    FileOutputStream fop = new FileOutputStream(f);
    File f = new File("textfile1.txt");
    System.out.print("Enter your name: ");
    String winner1 = input.readLine();

    System.out.print("Enter name1");
    String winner2 = input.readLine();
    System.out.print("Enter name2");
    String winner3 = input.readLine();
    System.out.print("Enter name3");
    String winner4 = input.readLine();
    System.out.print("Enter name4");


    if(f.exists()){
    String str= winner1;
    String str2= winner2;
    String str3= winner3;
    String str4= winner4;

    fop.write(str.getBytes());
    fop.write(str2.getBytes());
    fop.write(str3.getBytes());
    fop.write(str4.getBytes());


    fop.flush();
    fop.close();
    System.out.println("The data has been written");
    }

    else
    System.out.println("This file is not exist");
    }
    }
    Last edited by pirata; March 26th, 2011 at 07:07 PM.

  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Help here!!

    Welcome to the Forum, please read the welcome message in the forum announcements on how to ask for help.

    Well, variable f seems to be used before it's defined.
    once you fix that, you could use some polishing with the print statements, adding spaces where needed...
    also, I don't think you'll need the else block, the file was created on my computer.