• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling method not working

 
Sandeep Sagar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why it is not working , where i have been done mistake ?
public class Books {
private String book;
private int price;
private int ratings;
private String author;

public void bMethod(String book){
if(book == "Java" || book == "JAVA" || book == "java"){
price = 599;
this.book = book;
ratings = 5;
author = "Hills";
}
else if(book == "DBMS" || book == "Dbms" || book == "dbms"){
price = 799;
this.book = book;
ratings = 5;
author = "Rama Krishna";
}
}

public void setMethod(){
System.out.println("Book name is "+book+"\n Price is "+price+ "\n Rathing is "+ratings+ "\n Author is "+author);
}

}

import java.io.IOException;
import java.util.*;

public class Book extends Books {

public static void main(String[] args) throws IOException {
Book b = new Book();
Scanner scan = new Scanner(System.in);
System.out.println("Enter Book name Either Java or DBMS");
String str = scan.nextLine();
b.bMethod(str);
b.setMethod();
}

}

Output:
Enter Book name Either Java or DBMS
java
Book name is null
Price is 0
Rathing is 0
Author is null
 
Veerle Boer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have to learn how to compare strings in Java.
 
Sandeep Sagar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veerle Boer wrote:I think you have to learn how to compare strings in Java.

Thanks i got it
 
Veerle Boer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!

You could also look at the Java String API to see better ways to manage the case of a string, if you want to further improve your code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic