Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Help please

  1. #1
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default Help please

    I need to create a program that prints out my name 15 times in 15 lines, I can only make it print it one time with this code:


    Java Code:
    import java.io.*;
    
    public class Main {
    
    public static void main(String[] args)throws IOException{
        Writer output = null;
        //for (int x=0;x<10;x++);
        String text = "Javier Gonzalez";
        String text2 = "Javier Gonzalez";
    
        File file = new File("write.txt");
        output = new BufferedWriter(new FileWriter(file));
        output.write(text);
        output.close();
        System.out.println("Your file has been written");
        }
    }
    What can I do to make it print it 15 times or what code can I use ?
    I appreciate your help
    Last edited by Eranga; 12-16-2010 at 06:05 PM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    It wasnt working with the for loop either

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also please use code tags next time when you are posting code segments in the forum. Unformated codes are really hard to read.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by j187 View Post
    It wasnt working with the for loop either
    Because there is no body to execute in for loop.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    My bad im new in the forum, and also need to programming, I want it to be printed on the text file

  8. #8
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    so what do i need to change in the loop for it to run propperly

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you put like this,

    Java Code:
    for (int x=0;x<10;x++);
    there is nothing to do inside the for loop. Colon notify that the body is empty.

    Java Code:
    for (int x=0;x<10;x++) {
        // write into the text file
    }
    In the above code segment you have to write into the text file.

  10. #10
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    I modified the code like this putting the String text inside the for loop but it still doesnt work
    Java Code:
    import java.io.*;
    
    public class Main {
    
    public static void main(String[] args)throws IOException{
        Writer output = null;
        for (int x=0;x<10;x++){
        String text = "Javier Gonzalez ";
        }
        File file = new File("write.txt");
        output = new BufferedWriter(new FileWriter(file));
        output.write(text);
        output.close();
        System.out.println("Your file has been written");
        }
    }

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes it's not working. Why?

    Because in the for loop you've just initialize a String variable. Nothing else.

  12. #12
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    So what would i need to include in the loop for it to work, ive been trying but i still cant get it right

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay, in your code where you write something to the file? That's is what is the line of code that write something to the text file?

  14. #14
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    in the string text = ""; line

  15. #15
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    or so i thought

  16. #16
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    can anyone please help

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Create your file before the for loop, write the text into the file inside of the for loop and close the file after the for loop.

  18. #18
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    i used the PrintWriter to write the data but you can change it to the BufferedWriter if you want. here is the code

    Java Code:
    import java.io.*;
    
    public class Main {
    
    	public static void main(String[] args) throws IOException {
    
    		File file = new File("write.txt");
    		PrintWriter output = new PrintWriter(file);
    		String text = "Javier Gonzalez ";
    		// the text is written 10 times
    		for (int x = 0; x < 10; x++) {
    			output.println(text);
    		}
    		output.close();
    		System.out.println("Your file has been written");
    	}
    }

  19. #19
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    its printing the name 10 times but in the same line ?
    because I used the code and it only gives me one line

  20. #20
    j187 is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

Page 1 of 2 12 LastLast

Posting Permissions

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