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

Thread: File I/O Modify Text Problem

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default File I/O Modify Text Problem

    hello, i would really apreciate any help on this one.. i have just started file i/o in java and already the homework is bugging me.
    I am to write a program that reformats java source code from the next-line brace style to the end-of-line brace style. for example the following code :
    public class Test
    {
          public static void main(String[] args)
         {
           // some statements
         }
     
    }

    would be reformatted to :

     
    public class Test{
          public static void main(String[] args){
           // some statements
          }
     
    }
    my attempted code so far is below.. i don't know if i can use concat() in this instance and if i am even doing anything right... please help.. thanks.

     
    import java.io.*;
    import java.util.*;
     
    public class Main {
     
    	public static void main(String[] args) throws Exception {
     
    		File file = new File("C:\\JackTest\\test.txt");
    		format(file);
     
    	}
     
    	public static void format(File f) throws Exception {
     
    		Scanner input = new Scanner(f);
     
    		String[] lines = new String[7];
     
    		while (input.hasNext()) {
     
    			for (int i = 0; i < 7; i++) {
     
    				lines[i] = input.nextLine();
     
    				if (lines[i].startsWith("{")) {
     
    					lines[i-1].concat("{");
    					// i am stuck here.. 
     
    				}
     
     
     
    			}
     
    		}
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: File I/O Modify Text Problem

    In Java, strings are immutable (i.e. they can't be changed). You must re-assign the array the return value which holds the new string. Also, fyi Java defines the "plus" operator between strings as concatenating them.

    lines[i-1] += "{"; // short-handed "add then assign" also works

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: File I/O Modify Text Problem

    I would of thought that a reasonable approach to this would be to locate a \n followed by any number of whitespace characters followed by { and replace with just {

    Chris
    Last edited by Freaky Chris; November 26th, 2010 at 05:02 AM.
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. Open Text file
    By java_kiddy in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 5th, 2010, 02:52 AM
  2. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  3. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM
  4. how to modify a JList Frame?
    By JM_4ever in forum AWT / Java Swing
    Replies: 0
    Last Post: October 14th, 2009, 11:58 PM
  5. Java program to reduce spaces between the words in a text file
    By tyolu in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:17 AM