Using Scanner to find letter

Eric W

What I'm trying to do here is have a user input a sentence or some type of string, then have the scanner search through the string to determine if there is an "e" or "E" in there. If the scanner finds either one of those it will replace them with "xyz." If they are not present then it should return "All is good!" The code I have thus far is:

public class Example {
    String userInput;

    Scanner in = new Scanner(System.in);
    System.out.println("Please write a sentence.");
    userInput = in.nextLine();

    System.out.println(userInput.replace("e","xyz"));
    System.out.println(userInput.replace("E","xyz"));

    in.close();
}

As I think you can tell this really just prints the same line twice, once removing a lower case e and the other removing a capital E. I was looking for a way to combine the two replacements and then have it replace and print if it finds an e or just print "All is good!" if there are no "e"s.

Alexey Romanov

This isn't related to Scanner at all really. Trivial approach:

String replaced = userInput.replace("e","xyz").replace("E","xyz");
String out = replaced.equals(userInput) ? "All is good!" : replaced;
System.out.println(out);

Or use replaceAll:

Pattern eE = Pattern.compile("[eE]");
String replaced = eE.matcher(userInput).replaceAll("xyz");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I find the number of 8 letter words that do not contain the letter "e", using the grep command?

From Dev

Find 1 letter and 2 numbers using RegEx

From Dev

Using Scanner on Android

From Dev

Using RE scanner to find material in double quotes?

From Dev

Using Scanner in Java

From Dev

NoSuchElement using scanner twice

From Dev

Find letter in a String (charAt)

From Dev

Find a selector starting with a letter

From Dev

Using Scanner with sequence of Integers

From Dev

Using Python to find drive letter (Windows)

From Dev

Cannot find symbol in Java (Scanner)

From Dev

Create a Stream using Scanner

From Dev

Error using Scanner in java

From Dev

How to find the uppercase letter by using ascii code in JAVA?

From Dev

sql find name with letter

From Dev

implementing scanner using phonegap

From Dev

Simple Scan cannot find scanner

From Dev

Using predicate to find strings that start with a specific letter (C#)

From Dev

Using Scanner on Android

From Dev

Why do I get "cannot find symbol" using scanner when compiling?

From Dev

Using Scanner with sequence of Integers

From Dev

Can't Find File When Using Scanner to read In a Previously Created File

From Dev

Using a Scanner to read '(' and ')'

From Dev

Error using Scanner in java

From Dev

Using grep to find a word containing every letter a maximum of 1 times

From Dev

Input using Scanner Class

From Dev

String arrays using Scanner

From Dev

Find all words that not contain specefic letter in a string using regex

From Dev

Using Scanner for separating a string