0

Im working on the following task:

Write a method called normalizeText() which does the following:

-Removes all the spaces from your text

-Remove any punctuation (. , : ; ’ ” ! ? ( ) )

-Turn all lower-case letters into upper-case letters

-Return the result.

The call normalizeText("This is some \"really\" great. (Text)!?"

should return "THISISSOMEREALLYGREATTEXT".

Here is what i came up with:

import java.util.Scanner;

public class Crypto {

    public static void main(String[] args) {
        normalizeText();
    }

    public static String normalizeText() {
        Scanner input = new Scanner(System.in);
        System.out.println("Insert your Text : ");
        String crypto = input.next();

        crypto.replace("\\s", "");
        crypto.replaceAll("\\p{Punct}", "");
        crypto.toUpperCase();

        System.out.println(crypto);

        return crypto;
    }
}

For every sentence I input, I get just the first word of the sentence printed, without further String changes (Insert your Text : "asdfg asdf asd", returns "asdfg"). As you can see, I'm a beginner and asking for help in the form of hints.

Mohsen_Fatemi
  • 2,203
  • 2
  • 13
  • 23
Draz
  • 19
  • 3

1 Answers1

0

You need to use nextLine() instead of next() method, because next method only reads the first word of you sentence, that's why you see the first word in the output. you can read this link for more information.

String crypto = input.nextLine();

and about the wanted task for removing spaces and punctuations, i strongly recommend you to take advantage of Regex instead.

Mohsen_Fatemi
  • 2,203
  • 2
  • 13
  • 23