Lets say I have: import java.util.Scanner; public class As { public static void
ID: 3619630 • Letter: L
Question
Lets say I have: import java.util.Scanner;public class As
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter a line");
String line = sc.nextLine();
System.out.println("Enter the word to look for");
String word = sc.next(); In order to compare each character in 'line' with each character in 'string' Do I use:
for(int i = 0; i<line.length() ; i++)
{
if(line.charAt(i) == line.charAt(i); OR do i use instead if(line.charAt(i).equals(line.charAt(i));
Lets say I have: import java.util.Scanner;
public class As
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter a line");
String line = sc.nextLine();
System.out.println("Enter the word to look for");
String word = sc.next(); In order to compare each character in 'line' with each character in 'string' Do I use:
for(int i = 0; i<line.length() ; i++)
{
if(line.charAt(i) == line.charAt(i); OR do i use instead if(line.charAt(i).equals(line.charAt(i));
if(line.charAt(i).equals(line.charAt(i));
Explanation / Answer
Just do if(line.contains(word)) { //word exists in line } Although if you are wondering if you should use .equals or == with chars, you use == because char's are primitive data types (just like int, double, long, etc).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.