Results 1 to 7 of 7
  1. #1
    aejhyun is offline Member
    Join Date
    Aug 2015
    Posts
    1
    Rep Power
    0

    Default Duplicate a String

    If I were to input the String, "hello", the program should output, "hheelllloo". However, it's not working the way it should. Any suggestions on how to fix this? Also, if I don't initialize the variable duplicate and try to return it (if I don't try to return duplicate, then there is no error message), there is an error message that says "duplicate is not initialized." How come this error message is showing up?

    Java Code:
    import java.util.*;
    
    public class MyClass{
    
        public static void main(String args[]){
        	String userInput = "helloasdf"; 
        	MyClass strManipulation = new MyClass(); 
        	System.out.println(strManipulation.doubleChar(userInput)); 
        }
    
        public String doubleChar(String str){
        	String duplicate = null; 
        	String newString; 
        	
        	for(int i = 0; i < str.length(); i++){
        		duplicate = Character.toString(str.charAt(i)); 
        		duplicate = duplicate + duplicate; 
        		System.out.println(duplicate); 
        	}
        	return duplicate;
        }
    
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    18,253
    Rep Power
    27

    Default Re: Duplicate a String

    error message that says "duplicate is not initialized."
    Please copy the full text of the error message that shows what line the error is on.
    I see that duplicate is assigned a value so I don't understand where the error is happening.

    it's not working the way it should.
    Please copy the program's output and paste it here
    Add some comments saying what is wrong and show what the output should be.
    Last edited by Norm; 08-02-2015 at 08:40 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,669
    Rep Power
    7

    Default Re: Duplicate a String

    Well, the program is working the way it should (but not the way you want it to). You should probably make use of your variable "newString." You clearly had some reason for declaring it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Xen
    Xen is online now Member
    Join Date
    Jan 2015
    Posts
    53
    Rep Power
    0

    Default Re: Duplicate a String

    The way I understand you, you are complaining that if you do not initialize that variable, it will complain that the variable is not initialized.

    The compiler checks for error paths in which the variable is accessed without ever being initialized. The for loop is no guarantee that the variable, if not initialized in the beginning (with "null", as you do), will be or will have been initialized when it reaches the return statement.

    For this reason you need it to be initialized to null or perhaps to "".

    I don't know why your method doesn't work though. Personally I would use a StringBuilder instead of concatenating strings.

    Something like:

    Java Code:
            StringBuilder newString = new StringBuilder(); 
             
            for(int i = 0; i < str.length(); i++){
                char c = str.charAt(i);
                newString.append(c);
                newString.append(c);
            }
            return newString.toString();
    Also, I believe the other was right: in your for loop you assign the "aa" string (the doubled string) to duplicate, but you don't build up the new string that you want. To build up the new string you would have to do "newString = newString + duplicate" on each go, or "newString.append(duplicate)" (in the case of a StringBuilder). You can also write "newString.append("" + c + c)" in the case of a StringBuilder.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,669
    Rep Power
    7

    Default Re: Duplicate a String

    Quote Originally Posted by Xen View Post
    Also, I believe the other was right.... but you don't build up the new string that you want.
    Doesn't that mean the other way was wrong? In any case, my preference would be to use String.replaceAll().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Xen
    Xen is online now Member
    Join Date
    Jan 2015
    Posts
    53
    Rep Power
    0

    Default Re: Duplicate a String

    No, I meant that you were correct.

    You should probably make use of your variable "newString." You clearly had some reason for declaring it.
    In any case, my preference would be to use String.replaceAll().
    Nice one, but I think this was some programming exercise :p. How do you use the Java regexp? I still don't know... Can you perhaps give the short example of matching a single char and repeating it twice?

    I guess in some other regexp it would be something like "s/(\w)/\1\1/" but I have no clue about Java. I'm not even sure if that works what I just wrote.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,669
    Rep Power
    7

    Default Re: Duplicate a String

    Quote Originally Posted by Xen View Post
    I guess in some other regexp it would be something like "s/(\w)/\1\1/" but I have no clue about Java. I'm not even sure if that works what I just wrote.
    You're close.
    Java Code:
    UnaryOperator<String> t = s -> s.replaceAll("(.)", "$1$1");
    System.out.println(t.apply("Hello"));
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 6
    Last Post: 07-09-2015, 05:16 PM
  2. Remove Duplicate words from String
    By Alijavaprogrmammer in forum New To Java
    Replies: 6
    Last Post: 12-26-2013, 01:24 AM
  3. Remove Duplicate words from String
    By Alijavaprogrmammer in forum String Codes
    Replies: 4
    Last Post: 12-24-2013, 11:20 PM
  4. remove duplicate word in the string
    By katturv in forum New To Java
    Replies: 7
    Last Post: 12-19-2010, 08:16 PM
  5. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •