-1

I want to match input line1 String value and splitLine3 value and if match return True.

Now I got True and False as result if I run multiple times with the same code. I don't know what happened!

Here is output from VS -

PS C:\Users\User\Desktop\Learning Java> c:; cd 'c:\Users\User\Desktop\Learning Java'; & 'C:\Program Files\Java\jdk-16.0.2\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessacheese cheese burger cheese Yes No PS C:\Users\User\Desktop\Learning Java> c:; cd 'c:\Users\User\Desktop\Learning Java'; & 'C:\Program Files\Java\jdk-16.0.2\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'C:\Users\User\AppData\Roaming\Code\User\workspaceStorage\9c7b2b1e513c6038db76e183d731b494\redhat.java\jdt_ws\Learning Java_3455c92c\bin' 'PaizaRankC1' cheese cheese burger No No

import java.util.*;


public class PaizaRankC1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line1 = sc.nextLine();
        // String line2 = sc.nextLine();
        String line3 = sc.nextLine();
        String ans = null;
        String [] splitLine3 = line3.split(" ");
        // System.out.println(Arrays.toString(splitLine3));
        // System.out.println(line1.equals(splitLine3[0]));
        for (int i = 0; i < splitLine3.length; i++) {
            // System.out.println(line1.equals(splitLine3[i]));
            // System.out.println(i);
            if (line1.equals(splitLine3[i])){
                System.out.println(splitLine3[i]);
                ans = "Yes";
                System.out.println(ans);
                // break;
            }else{

                ans = "No";
                System.out.println(ans);
            }
        }

    }
}

I expected if it match return true how many times I run.

1 Answer 1

2

The first thing we're going to do is clean up your code because

  • It's excessive
  • It's hard to understand

I've updated the code

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter sentence to match: ");
        String wordToSearch = sc.nextLine();
        
        System.out.println("Enter sentence to be checked: ");
        String[] words = sc.nextLine().split(" ");
        
        for (String word : words) {
            
            if(line.equals(word))
                System.out.println(word + ": " + "Yes");
            
            else
                System.out.println("No");
        }

    }
}

I've done the following changes

  1. Removed the ans variable, the text is now directly printed out
  2. Removed all the comments
  3. Changed variable names, makes the code understandable
  4. Replaced the for loop with a for each loop
  5. Added print statements, helps the user to know what the program does

However these changes do not change the functionality of your code, so let's understand what it does.

  1. It asks the user to input a word, this wordis stored in wordToSearch.
  2. It asks the user to input a sentence, each " " (empty space) is split, which means the words in the sentence are stored in an array words
  3. It goes through each of the words one by one and checks if they are equal to wordToSearch

Let's use the example you gave

Enter word to searched for: cheese
Enter sentence to be checked: cheese burger
cheese: Yes

This program works fine, comment if you get any errors I'll help you resolve it!

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.