How to Capitalize the First Word After Reordering in Java?

  • Context: Comp Sci 
  • Thread starter Thread starter zzoo4
  • Start date Start date
  • Tags Tags
    Java String
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
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);

}
}
 
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?