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

Thread: What is wrong with my code?

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

    Default What is wrong with my code?

    I am trying to print this to a file:

            try {
     
            PrintWriter outfile = new PrintWriter(new FileWriter("CD.txt", true));
            outfile.println(title+","+singer+","+price+","+Category+","+discount + ",");
     
         for(int t=0;t<NumOfTracks;t++){
           Tracks[t]=(Newtrack);
           duration[t]=(Newtime);
           outfile.println(Tracks[t] + "," + duration[t]+ ",");
                t++;
            }
            outfile.close();
            }

    Whenever i print it to the file, i get
    title,singer,price,category,discount,track1,time1
    title,singer,price,category,discount,track2,time2

    I want it to be like this
    title,singer,price,category,discount,track1,time1, track2,time2.

    Why is it printing the same thing again and writting the track2 and time2 to it?

    Thank you


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Thumbs up Re: What is wrong with my code?

    outfile.println(title+","+singer+","+price+","+Cat egory+","+discount + ",");

    outfile.println(Tracks[t] + "," + duration[t]+ ",");

    try this

    use print instead of println

    this is just an confusing attempt.because anyhow if the above line is not in the loop ,then how it can be written twice.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Location
    Madurai
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    Hi friend,

    The variable 't' is incremented inside the 'for' loop. why? I'm just confused.
    Last edited by vrp; February 3rd, 2011 at 04:16 AM.
    with regards,
    Raja Pandi

  4. #4
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: What is wrong with my code?

    i m feeling like a dump
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: What is wrong with my code?

    Try DanBrowns solution. If this doesn't work, please post something we can compile.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 03:38 AM
  2. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM
  3. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM
  4. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 02:00 AM
  5. What can go wrong if you replace && with & in the following code:
    By scott01 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2010, 07:47 AM