java - Trying to read from text file using string.split -
i'm trying read through text file can find book name , output attributes, im getting wierd results. help?
this code:
system.out.print( "enter name of book: " ); input2 = scanner.nextline(); this.setname(input2); string bookname = getinfo.readline(); int = 0, num = 0; while(bookname != null) { string[] booknames = bookname.split("|"); i++; for(int j = (i*4-4); j<(i*4); j++) { if (booknames[j] == this.name){ num = 1; } } if(num == 1){ system.out.println(bookname); num = 0; } bookname = getinfo.readline(); }
this file :
candy mang|doodle|4586|45.0|
cradle|michael|1111|1.0|
this output:
press 1 add book, , 2 find book.
2
how search book?
1
enter name of book: candy mang
c
a
n
d
l
e
|
exit?
c
|
in regular expression used alternator(or). splitting "|"
same splitting ""
means split each character. that's reason have multiple character output. escape special symbol "\\|"
observation :
do not compare 2 objects using ==
have done booknames[j] == this.name
. instead use booknames[j].equals(this.name);
Comments
Post a Comment