what i am trying to do here, is to make it possible for the user to type 2 lines, and then say(or display) whether the string of the first sentence correspond to the string of the second sentence.

so for example:

first sentence:
i will go shopping

second sentence:
Tomorrow, i will go swiming.

" i will go" corresponds in the second sentence. so how can i make the program look for corresponding words, and identify them?

so far , i have written this. but i dont know how to finish.

#  1. import  java.io.*;    
#    2. class Ex11    
#    3. {    
#    4.     
#    5.               public static void main(String[] args)    
#    6.              {           
#    7.                     Console console=System.console();    
#    8.                     System.out.println("please write a sentence"+" ");    
#    9.                     String Str;    
#   10.                     Str=console.readLine();    
#   11.                        
#   12.     
#   13.                     System.out.println("please write another sentence"+" ");    
#   14.                     String Str2;    
#   15.                     Str2=console.readLine();    
#   16.                     char c=Str.charAt(0);    
#   17.                             
#   18.                          
#   19.                      int n1=0;    
#   20.                      int n2=0;    
#   21.                       while (n2<Str.length())    
#   22.                       {Str.charAt(n1);    
#   23.                       n1++;    
#   24.                             
#   25.                         
#   26.                              while (n2<Str2.length())    
#   27.                              {Str.charAt(n2);    
#   28.                               n2++;}    
#   29.                            
#   30.                        }    
#   31.                          
#   32.                        if(x.equals(x2))    
#   33.                         System.out.println(x);    
#   34.     
#   35.                         
#   36.              }    
#

how can i make the program look for corresponding words

Looks like you need to build an array of substrings of what is in the first line and check if any of them are in the second line. The longer ones being better matches.
For example Given: i will go shopping
i
i will
will
i will go
will go
etc

How does it work?

substrings?\

are you sure there is no other way.

i thought to make two variables

string end= " "

String Word= end+Str.charAt() + end

but how can i number each variable and make it look at the second string with no problem

If the substrings from the first line are in an array, you can loop thru the array and use the String.substring() method to see if any of the substring in the array is in the second line.

are you sure there is no other way

No, I'm sure there are many other ways. I gave you one.

i tried.

i found that if you add all the characters, you get no where.

Example
lets say i remove all the " " from Sentence A and B and compare between them.

i went to school yesterday
i went to a hospital yesterday

that would be:
iwenttoschoolyesterday
iwenttoahospitalyesterday

the computer wont identify similary charactaristics, cause the length of the string is different from the start. you wont be able to identify yesterday as similar, cause their character number is different.


The second thing that i discovered..
is that you cant isolate words and loop each one, that looping might continue for ever, cause i dont know how long a sentence can be


what i thought that would work is if i tried to define a variable called "Word"

i am working on it and i need help...
view plaincopy to clipboardprint?

1. Char End=" ";
2.
3. While(Str.charAt(n)<End)
4. {WordA=Str.charAt(n)
5. n++
6. }

Char End=" "; While(Str.charAt(n)<End) {WordA=Str.charAt(n) n++ }

here the pc would find teh first word. he will search till he finds an empty space and stops that would be the first word.

Then , may be to build another loop, that searches for the number of words in the sentence.

may be if i used the number End . that would tell me how many words there are in the sentence.

the way i could make him repeat the procedure while adding another word, is by using another variable

Str3+=word;


the problem that i found is that i cant put End as a number. i cant do :
int number;
number=Integer.parseIn(End)


i think it would work if i created a matrix of words...

Example:
I will leave home Tomorrow
I
will will
be
At
Work


thats is a good solution.

if anyone could help me with coming with a word variable
and printing the first sentence horizontally , i would be greatful

help me with coming with a word variable
and printing the first sentence horizontally

What is a word variable?
To print a sentence (what is a sentence?) horizontally (put all the words on one line)
use the print() statement for each word in the sentence and when you want to go to a new line use println("") or print("\n") to print the end of line character.

I have no idea what the rest of your post is about.

is space (" " ) a character or a string?

so far i wrote something like this:

import java.io.*;
class Wordy
{

              public static void main(String[] args)
             {       
                    Console console=System.console();
                    System.out.println("please write a sentence"+" ");
                    String Str;
                    Str=console.readLine();
                   

                    System.out.println("please write another sentence"+" ");
                    String Str2;
                    Str2=console.readLine();
                  
                    char End=" ";
                    int count=0;
                    String WordA;
                    String Words;
                    int n=0;
                    int n2=0;
                    int count2=0;
                    if(Str.charAt()==End){
                       count2=count2+1;                                        
                      }

                              while(n<String.length())
                             {
                                  while(Str.charAt(n2)<End)
                                  { WordA=Str.charAt(n2); 
                                    n2++;}
                                    Words=WordA+Words;  
                                    System.out.println(""+Words);   
                                    n++;
                                   
             
                             count=count+1; 
                             }     
                       
             }
}

but if " " character, i cant tell java to stop counting words before that space, can i ?


is there a way i could make java count all characters till " ", and then add all that character string to a variable called Word. then make java pass onto another String (after " ") find its characters till it reaches " ",
and add it to a word?
another variable
Words=Word+Words


is this direction possible? can it be done?

is there a way i could make java count all characters till " "

Yes. Several ways. Use the String.indexOf(" " ) method to find the location of the space.

then add all that character string to a variable called Word.

Use the String.substring() method to get the chars

The Scanner class will parse out words from a String.
The String.split() method will parse out words from a String.

This article has been dead for over six months. Start a new discussion instead.