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
Click For Summary

Discussion Overview

The discussion revolves around a Java programming problem where a user is attempting to reorder words in a string and capitalize the first word of the result. The scope includes programming techniques and string manipulation in Java.

Discussion Character

  • Technical explanation, Homework-related

Main Points Raised

  • A participant shares a Java program that reorders the first two words of a phrase to the end but struggles with capitalizing the first word of the output.
  • Another participant suggests using the StringBuffer class for string manipulation, noting that String objects are immutable and cannot be changed after initialization.
  • One participant expresses gratitude but requests further assistance.

Areas of Agreement / Disagreement

Participants have not reached a consensus on how to implement the capitalization feature, and the discussion remains unresolved regarding the best approach to achieve the desired outcome.

Contextual Notes

The discussion does not address specific methods for capitalizing the first letter after reordering, and there may be missing assumptions regarding the input format or expected output.

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 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
12K
  • · Replies 1 ·
Replies
1
Views
1K