Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//I am making a program where a user is supposed to input a sentence followed by a word. the program is then supposed to return whether the word is present or not followed by the position of the word
My code is giving false values. kindly help me spot the bug

Java
import java.io.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.util.*;

public class SMA391_Project {
public static void main (String [] args) throws java.lang.Exception{
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the Sentence");    
        String strOrig = in.nextLine(); //sentence
        String strOrig2=in.nextLine(); //word
        
  String [] splited = strOrig.split("\\b+");
 System.out.println(Arrays.asList(splited).contains("strOrig2"));
       int intIndex = strOrig.indexOf("strOrig");
        if(intIndex == -1){
            System.out.println(strOrig2+" not found");
        }else {
 System.out.println("Found "+strOrig2+" at index "  + intIndex);
        }
        }
        
    }


[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
Posted
Updated 15-Nov-15 21:30pm
v2
Comments
[no name] 16-Nov-15 3:16am    
You have to change your line from
int intIndex = strOrig.indexOf("strOrig");
to
int intIndex = strOrig.indexOf(strOrig2);
anaQata 16-Nov-15 3:19am    
thanks @PANKAJMAURYA However, the program prints: Enter the Sentence
I love programming
love
false
Found love at index 2
BUILD SUCCESSFUL (total time: 10 seconds).
How can i get rid of the false. Kindly
Richard MacCutchan 16-Nov-15 11:11am    
There is no line in the above code that prints the word 'false'.
OriginalGriff 16-Nov-15 3:30am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
Arasappan 16-Nov-15 4:16am    
HMM GOOD GRIFF

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900