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

Thread: Am I doing something wrong?

  1. #1
    Junior Member emkash01's Avatar
    Join Date
    Feb 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Am I doing something wrong?

    So I am trying to make a program where I can enter any phrase, and the computer detects the vowels in the phrase, changing them to a #. Can someone help me out because I think I'm pretty sure I am doing it right, but it doesn't come out right? I already have included a Scanner.
    System.out.println("Enter a phrase you like");
    String phrase = input.nextLine();
    int length = phrase.length();
    for(int z = 0 ; z < length; z++)
     
     
     
    { //Starts the loop
     
      if(phrase.charAt(z) == 'a' || phrase.charAt(z) == 'A' || phrase.charAt(z) == 'e' || phrase.charAt(z) == 'E' || phrase.charAt(z) == 'i' || phrase.charAt(z) == 'I' || phrase.charAt(z) == 'o' || phrase.charAt(z) == 'O' || phrase.charAt(z) == 'u' || phrase.charAt(z) == 'U'){
    System.out.print("#");
      }//ends if statement 1 
     
     
    }//ends the Loop
     System.out.print(phrase);


    So lets say I wrote: "John Doe is amazing"

    It comes out as:
    #######John Doe is amazing, meaning it knows the amount of vowels, but it wont replace them

    Its supposed to be:
    J#hn D## #s #m#z#ng
    Last edited by Norm; February 21st, 2019 at 06:05 PM. Reason: Clarification / Moved code tag to start of code

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

    Default Re: Am I doing something wrong?

    it doesn't come out right?
    What was the phrase that was entered?
    What was the expected output?
    What was the actual output?

    changing them to a #
    ...
    it wont replace them
    Where is the code to change a vowel to a #?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member emkash01's Avatar
    Join Date
    Feb 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing something wrong?

    Fixed it. I'm sorry about the code wrapping part. I'm new here.

    The code to change the vowel to a # is in the if statement. Its supposed to be that if any of those vowels are detected, then it changes them to a #. I think I did it wrong because it doesnt work completely. It puts the right amount of #, but it doesnt fill in for the vowel.

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

    Default Re: Am I doing something wrong?

    How is the code trying to change vowels to #s? I don't see any code that does that.
    What is the purpose of printing out the #s?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member emkash01's Avatar
    Join Date
    Feb 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing something wrong?

    I made it so that if the program detects any of the vowels, it prints out #. However, I dont know why it doesnt replace the vowels themselves with the #. I thought that if I add the "System.out.print" in the middle of the if statement, it would change the vowels, but it seems that is not the case. The program successfully figures out how many vowels there are, but it puts that amount in the beginning of the sentence.

    As for the purpose of this program, this is just something I am trying to understand outside of my course. I made this to practice on my logic in coding, although I cannot seem to figure out why this is happening.

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

    Default Re: Am I doing something wrong?

    it would change the vowels
    To change the value of a variable, there needs to be an assignment statement:
      theVariable = theNewValue;

    However, The contents of Strings can not be changed.

    To create a String with replaced values, the String class has a method that will do the replacement and create a new String with the replaced value.
    Look at the String class's API doc to see what methods are available
    http://docs.oracle.com/javase/8/docs/api/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member emkash01's Avatar
    Join Date
    Feb 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing something wrong?

    Are you suggesting something like:

     
     
    String question = "?";    
        System.out.print(question);
       }//ends if statement 1 
     
     
    }//ends the Loop
     
     
    System.out.print(phrase);

    and then expanding from there?

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

    Default Re: Am I doing something wrong?

    Are you suggesting something like:
    No, nothing like that. I don't see any way that code tries to use one of the String class's methods to create a new String with the replaced vowel.

    The code I am talking about to replace a char in a String would look like this:
      String newString = oldString.aStringMethod(args to the method);

    Read the API doc for the String class.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member emkash01's Avatar
    Join Date
    Feb 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing something wrong?

    I finally got it to work! Thank you so much. I think I understand it now.

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

    Default Re: Am I doing something wrong?

    Do you want to post your solution so others can see it?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What is wrong?
    By iMiTzi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 10th, 2014, 08:48 AM
  2. Replies: 4
    Last Post: March 6th, 2014, 05:14 PM
  3. [SOLVED] where I'm wrong?
    By arvindbis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 7th, 2012, 09:12 AM
  4. [SOLVED] I don't know what's wrong
    By Shivam24 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 25th, 2011, 08:54 AM
  5. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM

Tags for this Thread