• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compare Strings in reverse format  RSS feed

 
zoza noor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I am new in JAVA and have an assignment: I have to compare two Strings one in format (dd/MM/YYYY) and the other in format (YYYY/MM/dd) so how can I compare them?

actually I have two text files each one has lines of a strings consist of two part name and birthday (name/birth), so first I should divide the whole line into two tokens then compare the two file which their birth format are

opposite.



nairvana
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
What have you tried so far and where are you stuck?

Hint: Both formats you mentioned are dates.

Also this question will be better suited on the beginners forum. Moving...
 
zoza noor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
and thanks for response
below is code that I write , my main problem that one file has line formats like: Amanda Adam/12051990 and other file contains line like:Amanda Adam/19900512 you can see that the second part has a reverse of birth, so I appreciate if you can help me to compre the two files and print the common lines.

public class comparison {

ArrayList<Persons1> people1 = new ArrayList<Persons1>();
ArrayList<Persons2> people2 = new ArrayList<Persons2>();

public List<String> fileCompare() {
List<String> people = new ArrayList<String>();
try {
File myFile1 = new File("people1.txt");
FileReader FR1 = new FileReader(myFile1);
BufferedReader reader1 = new BufferedReader(FR1);
File myFile2 = new File("people2.txt");
FileReader FR2 = new FileReader(myFile2);
BufferedReader reader2 = new BufferedReader(FR2);

String line1 = null;
String line2 = null;
while (((line1 = reader1.readLine()) != null) && ((line2 = reader2.readLine()) != null)) {

if (line1.equals(line2)) {
System.out.println(line1);
people.add(line1);
}
}
reader1.close();
reader2.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return people;
}

void addPersons1(String LineToParse) {
String[] tokens = LineToParse.split("/");
Persons1 nextPersons1 = new Persons1(tokens[0], tokens[1]);
people1.add(nextPersons1);
}

void addPersons2(String LineToParse) {
String[] tokens = LineToParse.split("/");
Persons2 nextPersons2 = new Persons2(tokens[0], tokens[1]);
people2.add(nextPersons2);


}
}




 
zoza noor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Persons1 {

String name;
String birth;

Persons1(String n, String bd) {
name = n;
birth = bd;
}

public String getName() {
return name;
}

public String getBirth() {
return birth;
}
}





public class Persons2 {

String name;
String birth;

Persons2(String n, String bd) {
name = n;
birth = bd;
}

public String getName() {
return name;
}

public String getBirth() {
return birth;
}

}

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!