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

Thread: placing comma between strings in a line

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default placing comma between strings in a line

    I read a file containing declaration statements like int a b float a1 b1 in separate lines.
    now i should copy these read line into another file by separating variables with comma as we declare in programs.Am able to read from a file and copy to another file.but how to separate variables with comma?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    585
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: placing comma between strings in a line

    Split the line and add a comma after each token.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: placing comma between strings in a line

    Show us your code and the data, so we can advise you better.

  4. #4
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: placing comma between strings in a line

    public class first
    {
    public static void main(String args[])
    {
    String line,line1;
     String array[]=new String[20];
    BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\prog1\\file1.txt",true);
     StringBuffer  sb = new StringBuffer();
    Scanner scan=new Scanner(new File("F:\\prog\\file.txt"));
    while (scan.hasNext())
    {
    line=scan.next();
    line1=line.toLowerCase();
    array=line1.trim().spilt("\\s+"); 
    }
    sb.append(array[0]).append(" ");
    for(int i=1;i<array.length;i++)
     {
     
     sb.append(array[i]).append(",");
    }
     
    				bw.write(sb.toString());
    				bw.flush();
    				bw.close();
    }
    }
    when i write the above code i can insert comma between variables but i should end the line with semicoln instead i am ending with comma.

Similar Threads

  1. Java Class Excercise (Related to Strings + Cmd Line Arguments)
    By Wwong3333 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 19th, 2012, 09:22 PM
  2. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  3. How to remove comma before an String?
    By bhba73 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2011, 08:51 PM
  4. Sending large Strings ?! only sending a line
    By camel in forum Java Networking
    Replies: 2
    Last Post: April 19th, 2011, 12:41 PM
  5. How to remove the last comma
    By fride360 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 29th, 2011, 07:20 AM