-2

I have to write a program which prints the String which are inputed from a user and every letter like the first is replaced with "#":

mum    -> #u#
dad    -> #a#
Swiss  -> #wi##  //also if it is UpperCase
Albert -> Albert //no letter is like the first

The user can input how many strings he wants. I thought to split the strings with the Split method but it doesn't work with the ArrayList.

import java.util.*;

public class CensuraLaPrima {

    public static void main(String[] args) {
        Scanner s= new Scanner (System.in);
        String tdc;
        ArrayList <String> Parolecens= new ArrayList <String>();

        while (s.hasNextLine()) {
            tdc=s.nextLine();
            Parolecens.add(tdc);
        }

        System.out.println(Parolecens);
    }
}
8
  • I have tagged Java... Commented Apr 30, 2018 at 15:25
  • 2
    Your code shows no attempt... I don't understand where you try split and why ? What is your question ? how to look for letter like the first ? how to chenge this letter to # ? Or how to catch words entered by user ? Commented Apr 30, 2018 at 15:29
  • it doesn‘t work is no error Description. Add an error message or expected Data and what you get. Commented Apr 30, 2018 at 15:30
  • First, welcome. Second, Parolecens should not begin with an uppercase letter. IT's too confusing and not recommended. Third, please do more research and read the links that I will provide in my next comment. Also, this will help you a lot - Efficient way to replace chars in a string (java)? Commented Apr 30, 2018 at 15:32
  • Please, do more research then post what you've tried with a clear explanation of what isn't working and provide a Minimal, Complete, and Verifiable example. Read How to Ask a good question. Be sure to take the tour and read this. Commented Apr 30, 2018 at 15:32

3 Answers 3

1

If you want to read in single words you can use Scanner.next() instead. It basically gives you every word, so every string without space and without newline. Also works if you put in two words at the same time. I guess you want to do something like this. Feel free to use and change to your needs.

import java.util.*;
public class CensuraLaPrima {

  public static void main(String[] args) {
    Scanner s= new Scanner (System.in);
    String tdc;

    while (s.hasNext()) {
      tdc=s.next();
      char c = tdc.charAt(0);
      System.out.print(tdc.replaceAll(Character.toLowerCase(c) +"|"+  Character.toUpperCase(c), "#"));
    }


}

}

Edit: Basically it boils down to this. If you want to read single words with the scanner use .next() instead of .nextLine() it does consider every word seperated by space and newline, even if you put in an entire Line at once.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but the code should change every word, not only the first.
This is changing every word take it compile it and run it.
Sometimes it doesn't work: if i write test prova test uno due tre quattro it appears #es# prova #es# uno due #re qua##ro
@Lory sorry i did forget to paste Scanner.next() instead of nextLine() :D try it now. You're welcome
1

Tasks calling for replacing characters in a string are often solved with the help of regular expressions in Java. In addition to using regex explicitly through the Pattern class, Java provides a convenience API for using regex capabilities directly on the String class through replaceAll method.

One approach to replacing all occurrences of a specific character with # in a case-insensitive manner is using replaceAll with a regex (?i)x, where x is the initial character of the string s that you are processing:

String result = s.replaceAll("(?i)"+s.charAt(0), "#");

You need to ensure that the string is non-empty before calling s.charAt(0).

Demo.

Comments

-1

Assuming that you've successfully created the ArrayList, I'd prefer using the Iterator interface to access each elements in the ArrayList. Then you can use any String variable and assign it the values in ArrayList . Thereafter you can use the split() in the String variable you just created. Something like this:

//after your while loop
Iterator<String> it = Parolecens.iterator();

String myVariable = "";
String mySplitString[];

while(it.hasNext()) {
    myVariable = it.next();
    mySplitString = myVariable.split(//delimiter of your choice);
    //rest of the code
}

I hope this helps :) Suggestions are always appreciated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.