I'm trying to read through a text file so that i can find a book by name and then output its attributes, im getting wierd results. help?
This is the code:
System.out.print( "Enter name of book: " );
input2 = scanner.nextLine();
this.setName(input2);
String bookName = getInfo.readLine();
int i = 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 is the file :
candy mang|doodle|4586|45.0|
cradle|Michael|1111|1.0|
This is the output:
Press 1 to add a book, and 2 to find a book.
2
How would you like to search for the book?
1
Enter name of book: candy mang
c
a
n
d
l
e
|
Exit?
c
==as you have donebookNames[j] == this.name. Instead usebookNames[j].equals(this.name);