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: Strings, using \n deletes the string on that line.

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Strings, using \n deletes the string on that line.

    I am trying to write to a text file with information stored like this.

    brick 500
    stone 500

    And so on. Here is the code I am using to write to the file.

    		switch(record){
    		case "brick":
    			x.format("%s%n", "brick "  + value);
    			closeFile();
    		break;
    		case "log":
    			x.format("\n%s%n", "log " + value);
    			closeFile();
    		break;

    When I write like this, putting \n deletes the first line. Any ideas what I can look into or other functions that would be useful?


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    914
    Thanks
    0
    Thanked 170 Times in 151 Posts

    Default Re: Strings, using \n deletes the string on that line.

    You need to show us more code.
    For example, do you want to write only a single value to your file? Because you are closing the file after writing one entry.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Strings, using \n deletes the string on that line.

    	public void addRecords(String record, int value){
    		openFile();
    		System.out.println("Records added.");
    		switch(record){
    		case "brick":
    			x.format("%s%n", "brick "  + value);
    			closeFile();
    		break;
    		case "log":
    			x.format("\n%s%n", "log " + value);
    			closeFile();
    		break;
    		case "stone":
    			x.format("\n\n%s%n", "stone " + value);
    			closeFile();
    		break;
    		case "iron":
    			x.format("\n\n\n%s%n", "iron "  + value);
    			closeFile();
    		break;
    		case "coal":
    			x.format("\n\n\n\n%s%n", "coal " + value);
    			closeFile();
    		break;
    		case "copper":
    			x.format("\n\n\n\n\n%s%n", "copper " + value);
    			closeFile();
    		break;
    		}
    	}
    	public void openFile(){
    		try{
    			System.out.println("Formatter file open.");
    			x = new Formatter("save.txt");
    		}
    		catch(Exception e){
    			System.out.println("Error.");
    		}
    	}
    	public void closeFile(){
    		System.out.println("Formatter file closed.");
    		x.close();
    	}
    I'm pretty sure there is a way better way to do this.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    914
    Thanks
    0
    Thanked 170 Times in 151 Posts

    Default Re: Strings, using \n deletes the string on that line.

    Please read the API of the classes you want to use. The API of the Formatter class explicitely says that if you open a file the way you do the files contents will be deleted.

  5. The Following User Says Thank You to Cornix For This Useful Post:

    GregBrannon (July 28th, 2014)

Similar Threads

  1. GUI, textarea help, line by line string
    By tonynsx in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 19th, 2013, 01:55 PM
  2. [SOLVED] placing comma between strings in a line
    By shenaz in forum Java Theory & Questions
    Replies: 3
    Last Post: March 27th, 2013, 06:33 AM
  3. Replies: 11
    Last Post: November 12th, 2012, 01:09 PM
  4. 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, 10:22 PM
  5. compare two strings to see if they end with the same sub-string
    By tuathan in forum Loops & Control Statements
    Replies: 2
    Last Post: July 7th, 2012, 07:56 AM