I'm learning Java for a class and one of the assignments I need to do is implementing String methods into my code. After prompting the user to set text, I used a toLowerCase() method and printed it. On another line, I used a toUpperCase() method and printed it. Both printed correctly but whenever I used the toString() method, it only my text in lowercase.
Here is my main class:
import java.util.Scanner;
public class TalkerTester
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter some text: ");
String words = input.nextLine();
Talker talky = new Talker(words);
String yelling = talky.yell();
String whispers = talky.whisper();
System.out.println(talky);
System.out.println("Yelling: " + yelling);
System.out.println("Whispering: " + whispers);
}
}
Here is my class with all my methods
public class Talker
{
private String text;
// Constructor
public Talker(String startingText)
{
text = startingText;
}
// Returns the text in all uppercase letters
// Find a method in the JavaDocs that
// will allow you to do this with just
// one method call
public String yell()
{
text = text.toUpperCase();
return text;
}
// Returns the text in all lowercase letters
// Find a method in the JavaDocs that
// will allow you to do this with just
// one method call
public String whisper()
{
text = text.toLowerCase();
return text;
}
// Reset the instance variable to the new text
public void setText(String newText)
{
text = newText;
}
// Returns a String representatin of this object
// The returned String should look like
//
// I say, "text"
//
// The quotes should appear in the String
// text should be the value of the instance variable
public String toString()
{
text = text;
return "I say, " + "\"" + text + "\"";
}
}
I apologize for the long paste and my bad English.