Comp Sci How to Capitalize the First Word After Reordering in Java?

  • Thread starter Thread starter zzoo4
  • Start date Start date
  • Tags Tags
    Java String
AI Thread Summary
The discussion focuses on a Java program designed to reorder words in a phrase by moving the first two words to the end. The user seeks assistance in capitalizing the first word of the reordered result. It is noted that String objects in Java are immutable, which complicates direct modifications. A solution is suggested to use the StringBuffer class, which allows for mutable string operations. This approach can help achieve the desired capitalization after reordering the words.
zzoo4
Messages
42
Reaction score
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
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);
      
  }
}
 
thx...
But can you help me please?
 

Similar threads

Replies
7
Views
2K
Replies
12
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
7
Views
3K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
10
Views
11K
Replies
1
Views
1K
Back
Top