开发者

UpperCase problem, using IndexOf

开发者 https://www.devze.com 2023-02-07 13:57 出处:网络
Enter a line of text. No punctuation please. Java is the language. I have rephrased that line to read: Is the language java.

Enter a line of text. No punctuation please.

Java is the language.

I have rephrased that line to read:

Is the language java.

Attempt:

 int x;
 String  sentence, first;

 System.out.println("\nEnter a line of text. No punctuation please.");

 Scanner keyboard = new Scanner (System.in);

 sentence=keyboard.nextLine();

   x = sentence.indexOf(" ");
 first= sentence.substring(0,x);
 second=sentence.substring(0,1)
 second=second.toUpperCase();  
 System.out.println("I have rephrased that line to read:");
 System.out.println(second+sentence.substring(x+1)+" "+first);

Output:

Enter a line of text. No punctuation plea开发者_如何学Gose.

what is going on

I have rephrased that line to read:// It should read " Is going on what"

W s going on what

P.S -I need to make letter "i" capital. How can I make "second.substring(0,1)" read character "i"? As suggested, I tried to figure out the stripping the letter and concatenating it with the uppercase but I am not sure.

To people lashing at me for not doing this on my own. I have given it multiple attempts and read through the book. It was of no help. I had to ask the professor so he gave me a head start but still not sufficient. By all means, I want to understand it. Not everyone grasps the same way. I personally, need examples and elaborate explanations to comprehend.


Change:

   first= sentence.substring(0,4);  

To:

   first= sentence.substring(0,x);

Then add:

   sentence = sentence.substring(x+1,sentence.length());


Try:

first = sentence.substring (0, x);
// other stuff
System.out.println (sentence.substring (x + 1) + " " + first);


TL;DR: Use another call substring to strip the first part. See the String class documentation and also Manipulating Characters in a String


Your code does not seem to be working at all. You save in x the position of the first space but don't use that, it should be sentence.substring(0, x).

For the sentence: how are you. In first you'll have stored the first word: how

Then you should strip from sentence the first word with something like this: sentence = sentence.substring(x+1). Now sentence will be: are you

After that just concatenate both:

String newSentence = sentence + " " + first;

And there you have stored in the variable newSentence: are you how

God bless!

0

精彩评论

暂无评论...
验证码 换一张
取 消