0

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.

Matt
  • 13
  • 3
  • 5
    What do you think the point of `text = text` is in `toString`? It doesn't do anything – Michael Jan 07 '20 at 12:59
  • Always include the actual and expected output. Not just a description "not working". Show the exact output you get! And yes, your toString method just shows the text, without any uppercase?" – GhostCat Jan 07 '20 at 13:00

2 Answers2

6

It is because you modify the value of text. It will persist even after you return. You should not. Instead, directly return like this:

String yell() {
    return text.toUpperCase();
}
CoderCharmander
  • 1,862
  • 10
  • 18
0

Using your methods yell() and whisper, you also edit your variable text. In fact, before the line

    System.out.println(talky);

you have used the method whisper which make the variable text into lowercase.

You have to edit your code like this:

public String whisper()
    {
        return text.toLowerCase();
    }

public String yell()
    {
        return text.toUpperCase();
    }

public String toString()
    {
        return "I say, " + "\"" + text + "\"";
    }

Moreover, to be more precise, use the Java keyword this when you use the variable text! For example,

public Talker(String startingText)
    {
        this.text = startingText;
    }
Liuk
  • 351
  • 1
  • 13