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

Thread: Need help with replace function

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with replace function

    My code(pasted below) basically replaces A, B, C with C, P, Q.
    My new requirement is, I am going to create a file and then it has to replace the A, B, C of file content with C, P, Q. How do I do it?
    for example: let's say the name of the file is the filename.txt.So what changes do I have to make to existing code, please help

    public class replacefunction1 {
     
     
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		 File file = new File("filename.txt"); 
     
     
     
    	    HashMap<Character, Character> replacements = new HashMap<>();
    	    replacements.put('a', 'c');
    	    replacements.put('b', 'p');
    	    replacements.put('c', 'q');
     
    	    String input =  "abcd";
    	    StringBuilder output = new StringBuilder();
    	    for (Character c : input.toCharArray()) {
    	        output.append(replacements.getOrDefault(c, c));
    	    }
    	    System.out.println(output.toString());
     
     
     
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with replace function

    Also posted at: https://www.dreamincode.net/forums/t...lace-function/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to replace the unicodes in java using keytyped or keypressed function
    By Mian Wahab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 24th, 2017, 04:44 PM
  2. Replace unless inside quotes?
    By sci4me in forum Java Theory & Questions
    Replies: 1
    Last Post: August 24th, 2014, 05:11 PM
  3. Replace bracket
    By marcobelli in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 4th, 2013, 12:00 PM
  4. Using the replace method?
    By jean28 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 22nd, 2012, 04:17 PM
  5. [SOLVED] Replace Phrase in String
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2010, 12:10 PM