public class Book {
private String Author;
private Book[]Details;
public void setAuthor()
{
}
public String getAuthor()
{
return Author;
}
//Author already set for Array
public int getviewInfo()
{
System.out.println("Enter author");
Scanner kb= new Scanner(System.in);
int index=0;
String author= kb.next();
for(int i=0 ;i<Details.length; i++)
{
if((Details[i].getAuthor()).equals(author)); //compare both author
{
index=i;
//loop enters here even when author input not equal to getAuthor, why??
System.out.println("SAME author"); //& will continue to run this i times
}
}
System.out.println("sameindex "+index); //therefore index is wrong
return index;
}
I have a method getviewInfo()
which suppose to get index position of the array object for printing later. It prompt user to enter a String 'author', take it and compare for equality with another String which has been set earlier for the Array.
Problem is, inside the for loop, the if statement to compare these 2 strings will enter even when String author input is different than the other String from Details[i].getAuthor()
, an the index i gotten is wrong.
Why isn't the if statement working correctly? can someone tell me what am I doing wrong? Thank so much