0

I'm doing a library system which once I input a book name, and found inside the array, the out put would be "the book was returned" . But every time I input the name of one the books listed in the array, it still say that "the book is out of order". How can I solve this problem?

import java.util.Scanner;
public class NewClass {
    public static void main (String args[]){
        Scanner book = new Scanner(System.in);
        String [] library = new String [4];
        library [0] = "Brazil";
        library [1] = "Japan";
        library [2] = "China";
        library [3] = "India";       

        String bookEntry = " ";
        int day;
        int x = 2;
        int penalty;
         for (int i = 0; i < library.length; i++){
        System.out.println("Insert name of the book: ");
        bookEntry= book.next();

           if (bookEntry == library[i]){
               System.out.println("The book was returned");
           }else if (bookEntry != library[i]){ 
               System.out.println("The book is out of order");

System.out.println("\n" + bookEntry.toUpperCase()+ " " + "is out since: ");
day = book.nextInt();

if (day > x){
    penalty = day*20;
    System.out.println("Total fine: " + penalty);
}else{
    System.out.println("Not yet due.");
}
       }


    }
    }
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
denmark16
  • 29
  • 7
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Bernhard Barker Jul 05 '15 at 12:41

1 Answers1

1

In Java, you don't compare 2 strings by ==.

Instead you use, .equals() method of class String.

  • ==            -> is a reference comparison, i.e. both objects point to the same memory location
  • .equals()  -> evaluates to the comparison of values in the objects

More on this here

Corrected code below :

import java.util.Scanner;

public class NewClass {

public static void main(String args[]) {
    Scanner book = new Scanner(System.in);
    String[] library = new String[4];
    library[0] = "Brazil";
    library[1] = "Japan";
    library[2] = "China";
    library[3] = "India";
    String bookEntry = " ";
    int day;
    int x = 2;
    int penalty;
    System.out.println("Insert name of the book: ");
    bookEntry = book.next();
    boolean present = false;
    for (int i = 0; i < library.length; i++) {
        if (bookEntry.equals(library[i])) {
            present = true;
            break;
        }
    }
    if (present) {
        System.out.println("The book was returned");
    } else {
        System.out.println("The book is out of order");
        System.out.println("\n" + bookEntry.toUpperCase() + " " + "is out since: ");
        day = book.nextInt();
        if (day > x) {
            penalty = day * 20;
            System.out.println("Total fine: " + penalty);
        } else {
            System.out.println("Not yet due.");
        }
    }
}
}

Working code here

UPDATE : I have changed the code.

Community
  • 1
  • 1
Shrinivas Shukla
  • 4,325
  • 2
  • 21
  • 33
  • Sorry Sir but the problem remains unsolved. I input book names from the array list and still the output is "out of order". What I really want is when I input a book name that included in the array, the output would be "The book was returned" System.out.println("Insert title of the book: – denmark16 Jul 05 '15 at 12:00
  • I just changed the .next to ".nextLine"...and now it perfectly works. Thank you very much for the help – denmark16 Jul 05 '15 at 12:26
  • You made this change in you original code or in the code I provided? – Shrinivas Shukla Jul 05 '15 at 12:27
  • from my original code Sir...but your code is of course the biggest help :) – denmark16 Jul 05 '15 at 12:57