-4

Here is the full Question: ... My Code

import java.util.Scanner;

import java.lang.StringBuilder;

public class javaAPIString {

    public static void main(String[]args) 
    {
        String SentenceFromUser = "";
        String IndiWordFromUser = "";
    /// String upper = IndiWordFromUser.toUpperCase();
        //String[] words = SentenceFromUser.split("\\s+");

        char ans;   

          Scanner keyboard = new Scanner(System.in);
          do
            {
              final StringBuilder result = new StringBuilder(SentenceFromUser.length());
              String[] words = SentenceFromUser.split("\\s");
              System.out.println("Enter a sentence :");
              SentenceFromUser = keyboard.nextLine();

              System.out.println("Enter a word : ");
              IndiWordFromUser = keyboard.next();
       ///    IndiWordFromUser = upper;

              for(int i =0; i > words.length; i++ )
              {
                  if (i > 0){
                        result.append(" ");
                        }   
                        result.append(Character.toUpperCase(words[i].charAt(0))).append(
                                    words[i].substring(1)); 
              }

      //    System.out.println("The Output is : " + SentenceFromUser);


              System.out.println("Do you want to enter another sentence and word ? If yes please type 'Y' or 'y'.");
              ans = keyboard.next().charAt(0);
            }
            while ((ans == 'Y') || (ans == 'Y')); 
    }
}

My Output/ problem of my code : Enter a sentence : i like cookies Enter a word : cookies The Output is : i like cookies Do you want to enter another sentence and word ? If yes please type 'Y' or 'y'.

it returns the same original sentence without changing to Uppercase of the second input to replace to all CAPS.

  • 1
    So, did you have a question? Perhaps there is an error with your code that you wanted to include in your post? – azurefrog Sep 26 '17 at 21:42
  • 2
    You showed the assignment - you showed your code. Now you need to tell us what the problem is and ask a specific question about your code. – takendarkk Sep 26 '17 at 21:43
  • thanks for that , i have the problem with my output, it display the original sentence , example : Enter a sentence : i like cookies Enter a word : cookies The Output is : i like cookies Do you want to enter another sentence and word ? If yes please type 'Y' or 'y'. – MELA.L Math Sep 26 '17 at 21:44
  • `String IndiWordFromUser = ""; String upper = IndiWordFromUser.toUpperCase();` Trying to uppercase an empty string is probably not what you want here. Also, you seem to build a `result` string, but then you just print the original input anyway `System.out.println("The Output is : " + SentenceFromUser);`. You never use `result` anywhere. – takendarkk Sep 26 '17 at 21:45
  • thank you for that , i see what you mean , that would display the original input. But if i take that out, i get the following Enter a sentence : i like chicken Enter a word : chicken Do you want to enter another sentence and word ? If yes please type 'Y' or 'y'. – MELA.L Math Sep 26 '17 at 21:50

2 Answers2

0

Just some things:

  1. please use java naming conventions for variables name
  2. when you execute your code this instruction will overwrite the user input with an empty string.

    IndiWordFromUser = upper;

  3. String[] words = SentenceFromUser.split("\\s");... you are splitting an empty string the first time and the old sentence the other runs

  4. The variable IndiWordFromUser is never read

TheOni
  • 810
  • 9
  • 26
0

I hope my solution will help you out! :)

import java.util.Scanner;

public class Solution {

    private static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
       getTextFromUser();
    }

    private static void getTextFromUser() {
       print("Enter Sentence Here: ");
       String text = in.nextLine();

       print("\nDo you want to capitalize words in sentence? Y/N: ");

       while (in.nextLine().equalsIgnoreCase("y")) {
           print("Enter Word: ");
           print("Modified Text: " + changeWord(text, in.nextLine()));
           print("\n\nDo you want to capitalize words in sentence? Y/N");
       }
    }

    private static String changeWord(String text, String word) {
        return text.replaceAll("\\b" + word + "\\b" /* \\b Means word 
        boundary */, word.toUpperCase()); // No validations done.
    }

    private static void print(String message) {
        System.out.print(message);
    }

}
Yasin
  • 76
  • 4
  • thank you so much @Yaz33n , I am literally crying of joy. I can't thank you enough. You are my hero. I can't even, express my gratitude. – MELA.L Math Sep 27 '17 at 04:04