Capitializing the java String?

  • Comp Sci
  • Thread starter zzoo4
  • Start date
  • Tags
    Java String
In summary, the program is designed to rearrange words in a user-inputted phrase and add a prefix, "Yoda: ", to the output. The user is having trouble capitalizing the first letter of the results section after the words have been rearranged. To solve this, it is recommended to use the StringBuffer class which allows for manipulation of characters in a string.
  • #1
zzoo4
42
0
Like this is my program to change the words around in order. Like First two words go to the back. What I am having trouble is, How do I capitalize the first at the results section after
the words order have changed??
This is my program.


import java.util. Scanner;

public class Starwars{
public static void main (String[]args){

//Declare Variables Section
int length, position=0;
String phrase, phrase2, phrase3;
Scanner input= new Scanner(System.in);

//Obtain Input From Section
System.out.println ("Enter your phrase: ");
phrase= input.nextLine();
length= phrase.length();
System.out.println (phrase);

//Perform Calculations Section
int i=0;
int i2=0;

while (i<length){

if (phrase.substring(i,i+1).equals(" ")){
i2++;
if (i2==2){
position=i;
}
}



i++;

}

phrase2=phrase.substring(position);

phrase3=phrase.substring(0,position);


System.out.println ("Yoda: " + phrase2 + ", " + phrase3);

}
}
 
Physics news on Phys.org
  • #2
When you post code, please put [noparse]
Code:
 and
[/noparse] tags around it. I have done that for your code.
zzoo4 said:
Like this is my program to change the words around in order. Like First two words go to the back. What I am having trouble is, How do I capitalize the first at the results section after
the words order have changed??
This is my program.

Code:
import java.util. Scanner;

public class Starwars{
  public static void main (String[]args){
    
    //Declare Variables Section
    int length, position=0;
    String phrase, phrase2, phrase3;
    Scanner input= new Scanner(System.in);
    
    //Obtain Input From Section
    System.out.println ("Enter your phrase: ");
    phrase= input.nextLine();
    length= phrase.length();
    System.out.println (phrase);
    
    //Perform Calculations Section
    int i=0;
    int i2=0;
    
    while (i<length){
      
      if (phrase.substring(i,i+1).equals(" ")){
        i2++;
        if (i2==2){
          position=i;
        }
      }
      
      
      
      i++;
      
    }
  
    phrase2=phrase.substring(position);
    
    phrase3=phrase.substring(0,position);
    
  
    System.out.println ("Yoda: " + phrase2 + ", " + phrase3);
      
  }
}
 
  • #3
thx...
But can you help me please?
 
  • #5


I would recommend using the built-in method `toUpperCase()` in the `String` class to capitalize the first letter of the resulting phrase. This method converts all characters in the string to uppercase, so you may need to use the `substring()` method to isolate the first letter and concatenate it back into the phrase. You can also use the `charAt()` method to access a specific character in the string and use the `toUpperCase()` method on that character. Additionally, make sure to properly handle any punctuation or special characters that may be present in the original phrase.
 

1. How do I capitalize the first letter of a string in Java?

To capitalize the first letter of a string in Java, you can use the substring() and toUpperCase() methods. Here is an example: String str = "hello"; str = str.substring(0, 1).toUpperCase() + str.substring(1); This will return the string "Hello".

2. Can I capitalize all letters in a string using Java?

Yes, you can capitalize all letters in a string using the toUpperCase() method. Here is an example: String str = "hello"; str = str.toUpperCase(); This will return the string "HELLO".

3. How do I capitalize each word in a string using Java?

To capitalize each word in a string, you can use the split() and toUpperCase() methods. Here is an example: String str = "hello world"; String[] words = str.split(" "); for (int i = 0; i < words.length; i++) { words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1); } str = String.join(" ", words); This will return the string "Hello World".

4. Can I capitalize a string without changing the original string?

Yes, you can use the charAt() and toUpperCase() methods to create a new string with the first letter capitalized. Here is an example: String str = "hello"; String newStr = Character.toUpperCase(str.charAt(0)) + str.substring(1); This will return the string "Hello", while leaving the original string "hello" unchanged.

5. How do I handle strings with multiple words and special characters?

To handle strings with multiple words and special characters, you can use the Character.isLetter() method to check if a character is a letter before capitalizing it. Here is an example: String str = "hello, world!"; String[] words = str.split(" "); for (int i = 0; i < words.length; i++) { if (Character.isLetter(words[i].charAt(0))) { words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1); } } str = String.join(" ", words); This will return the string "Hello, World!".

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
772
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top