0

I'm trying to see if a string contains a specific character in it. For example, given the text file:

I am going to the store. 
I will be at <area>.
I need to buy <item>.

I'm trying to have my program scan the text line-by-line and detect any instance of "<" and ">". That way it can remove these and prompt the user for an "area" or "item" in the console. The current block of code I have is as follows:

Scanner sc = new Scanner(f);
while(sc.hasNextLine()) {
    String info = sc.nextLine();
    if(info.contains('<')) /// this is where I am stuck! Contains() doesn't seem to work on a single char
}
dtwomey
  • 11
  • 5
  • 3
    *this is where I am stuck! Contains() doesn't seem to work on a single char* - read the API for the String.contains(...) method to see what the parameters are for the method. You can't program if you don't use/read the API. – camickr Nov 20 '20 at 01:48
  • @geocodezip Not quite a duplicate, as this Question is specific to use a `char` type inappropriately. – Basil Bourque Nov 20 '20 at 02:09
  • That is covered by the answers in that question (at least in my opinion), but it was just a suggestion. – geocodezip Nov 20 '20 at 02:35

2 Answers2

1

char is not CharSequence

Your call to String::contains takes a CharSequence such as String while you are passing a char. Two different types.

Use the double-quote mark rather than single-quote mark around < to get a String object rather than a char primitive: "<" versus '<'.

boolean containsLessThan = input.contains( "<" ) ;

If your goal is simply deletion of such characters, use String::replace.

String result = input.replace( "<" , "" ).replace( ">" , "") ;

FYI, the char type is obsolete, unable to represent even half of the characters defined in Unicode.

Here is a chart I created showing the various text-related classes bundled with Java. String is one of several classes that implement the CharSequence interface. The char type is not included here because it is (a) a primitive rather than a class, and (b) obsolete.

Diagram of String, CharSequence, and related classes.

By the way, recommended reading: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

You can use info.indexOf('<')

it returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

So this would be your code: if(info.indexOf('<') > -1)

Strong Recommendation: Please first search enough and/or read the right documentation for each part of the code that you're working with or each time get stuck. Asking question would be the last choice when you absolutely devistated. Here is exactly what you asked for example:

https://stackoverflow.com/questions/506105/how-can-i-check-if-a-single-character-appears-in-a-string#:~:text=You%20can%20use%20string.,the%20character%20does%20not%20occur.

erfoon
  • 142
  • 1
  • 10