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: Doubling Letter in a String -- Stuck on Code

  1. #1
    Junior Member
    Join Date
    Jul 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Doubling Letter in a String -- Stuck on Code

    Hey guys! So I'm asked to make a piece of code that takes a string of text that a user inputs and print to the console the same string but with each letter doubled and each '!' tripled.

    Example: hey, guys! ---> hheeyy, gguuyyss!!!

    I have made the code below so far, and it works, however I need to be able to triple the '!' while having commas and other special characters remain at 1! I have tried adding more if-statements but it seems to only break the output even more. Any help would be appreciated. Thank you!!!

    So far, I will input "hey, guys!" and receive "hheeyy, gguuyyss!" (I need the '!' tripled without doubling the comma.

     
    import java.util.Scanner;
     
    public class DoubleLetters {
     
    	public static void main (String[] args) {
     
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Please type a sentence:");
    		String input = scan.nextLine();
     
    		int length = input.length();
    		String newString = "";
     
    		for(int j = 0; j < length; j++) {
    			if(Character.isLetter(input.charAt(j)))
    			        newString = newString + input.charAt(j) + input.charAt(j);
    			else
    				newString = newString + input.charAt(j);
    		}
    			System.out.println(newString);
    	}
    }


    --- Update ---

    I've been thinking and I think the only way I can do this is with another if-statement. I'm not sure if I should put it right after the first if-statement or after the else

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Doubling Letter in a String -- Stuck on Code

    do this is with another if-statement
    Yes, you would need add another if to detect the !
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to delete first letter of string
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 5th, 2013, 05:03 PM
  2. How to capitalize a letter in String?
    By garbage in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 12th, 2013, 09:03 AM
  3. [SOLVED] Hangman Program Java: Comparing the User's Guess with Another String Letter by Letter
    By theonlydvr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 29th, 2013, 05:35 PM
  4. Finding the count of a letter in a String?
    By vlkn448 in forum Algorithms & Recursion
    Replies: 7
    Last Post: July 15th, 2012, 11:55 AM
  5. Incrementing every letter in a string that occupies an odd position.
    By haktheplanet in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2010, 04:45 AM

Tags for this Thread