-2

So I have spent a whole day working on a little java project and thought I should try asking here before I spend another three hours googling.

So what I am trying to do is use binary search in an if-else statement to access an array that is connected to a class somewhere else in the code.

and so you can see the code:

public class BankProject {

public static void main(String[] args) {

Account[] accountArray = new Account[10];
accountArray[0] = new Account("Dillen Newton", 5555, 9000000.0, 10.0);
accountArray[1] = new Account("Meteor", 5556, 10000.0, 5.5);
accountArray[2] = new Account("Meteorite", 5557, 20000.5, 20.0);
accountArray[3] = new Account("Super Mario", 5558, 100.0, 7.0);
accountArray[4] = new Account("A Person", 5559, 234567.0, 6.0);
accountArray[5] = new Account("Noone", 5560, 97498237.99, 50.0);
accountArray[6] = new Account("Yes", 5561, 5.5, 100.0);
accountArray[7] = new Account("Anonymous", 5562, 222.2, 13.0);
accountArray[8] = new Account("Five Hours", 5563, 7600.0, 40.0);
accountArray[9] = new Account("Who Knows", 5564, 9533.8, 99.0);

Scanner reader = new Scanner(System.in);  // Reading from System.in
System.out.println("Please Enter A Specified Name");
System.out.println("Dillen Newton");
System.out.println("Meteor");
System.out.println("Meteorite");
System.out.println("Super Mario");
System.out.println("A Person");
System.out.println("Noone");
System.out.println("Yes");
System.out.println("Anonymous");
System.out.println("Five Hours");
System.out.println("Who Knows");
System.out.println("");
String n = reader.next(); // Scans the next token of the input as an int.

    if (n.equals("Dillen Newton")){
        Arrays.binarySearch(accountArray, "Dillen Newton");
        System.out.println("Welcome, Dillen");
} else if (n.equals("Meteor")){
        Arrays.binarySearch(accountArray, "Meteor");
        System.out.println("Welcome, Meteor!");     
} else if (n.equals("Meteorite")){
    Arrays.binarySearch(accountArray, "Meteorite");
    System.out.println("Hello, Meteorite!");        
} else if (n.equals("Super Mario")){
    Arrays.binarySearch(accountArray, "Super Mario");
    System.out.println("Welcome, Mario");       
} else if (n.equals ("A Person")){
    Arrays.binarySearch(accountArray, "A Person");
    System.out.println("Hi Person!");       
} else if (n.equals("Noone")){
    Arrays.binarySearch(accountArray, "Noone");
    System.out.println("Welcome, Nobody!");     
} else if (n.equals("Yes")){
    Arrays.binarySearch(accountArray, "Yes");
    System.out.println("Yes");  
} else if (n.equals("Anonymous")){
    Arrays.binarySearch(accountArray, "Anonymous");
    System.out.println("Hello There!");     
} else if (n.equals("Five Hours")){
    Arrays.binarySearch(accountArray, "Five Houres");
    System.out.println("Has it been that long already?");       
} else if (n.equals("Who Knows")){
    Arrays.binarySearch(accountArray, "Who Knows");
    System.out.println("I don't");      
} else{
    System.out.println("Incorrect Account. Please restart and try again");
    reader.close();
}


    /*  Account testAccount = new Account("Dillen Newton", 1122, 20000.0, 4.5);
    testAccount.withdraw(2500);
    testAccount.deposit(3000);
    java.util.Date dateCreated = new java.util.Date();
    System.out.println("Date Created:" + dateCreated );
    System.out.println("Name:" + testAccount.getName());
    System.out.println("Account ID:" + testAccount.getId());
    System.out.println("Balance:" + testAccount.getBalance());
    System.out.println("Monthly Interest:" + testAccount.getMonthlyInterest());

    System.out.println("Process completed.");
    }
    */
} 

}

class Account {

private String name;
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;

public Account(){
    name = "";
    id = 0;
    balance = 0.0;
    annualInterestRate = 0.0;
}

public Account(int newId, double newBalance){
    id = newId;
    balance = newBalance;
}

public Account(String newName, int newId, double newBalance, double newAnnualInterestRate) {
    name = newName;
    id = newId;
    balance = newBalance;
    annualInterestRate = newAnnualInterestRate;
}

public String getName(){
    return name;
}

public int getId(){
    return id;
}
public double getBalance(){
    return balance;
}
public double getAnnualInterestRate(){
    return annualInterestRate;
}

public void setName(String newName){
    name = newName;
}

public void setId(int newId){
    id = newId;
}
public void setBalance(double newBalance){
    balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate){
    annualInterestRate = newAnnualInterestRate;
}
public Date getDateCreated(){
    return dateCreated;
}
public double getMonthlyInterestRate(){
    return annualInterestRate / 12;
}
public double getMonthlyInterest(){
return (balance*getMonthlyInterestRate()/100);
}

public void withdraw(double amount)
{
    balance = balance - amount;
}

public void deposit(double amount)
{
    balance = balance + amount;
}
}

So I believe that everything so far is correct. But the if-else statement is only considering everything as Incorrect.

What I am trying to do is select an account from the array so that in the next part I can start doing things like withdrawals and deposits and balance checking. Either linear search or binary search it necessary for this project.

I don't know if I need to try something else, or if I am just missing something really simple. Anyway, any help would be appreciated!

  • 1
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Michael Markidis Jun 12 '16 at 06:22
  • To expand on Michael's comment: == and equals() do different things. http://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java – Locke Donohoe Jun 12 '16 at 06:28

2 Answers2

0

comparing String using n.equals("Dillen Newton")

Jason Christ
  • 125
  • 7
  • Well thanks for helping on that front. Will update the main code soon, but I am still receiving incorrect answers and now exceptions from the if statements. – TheConfusedMeteor Jun 12 '16 at 06:32
0

Title of your question is misleading. Your question expressed in last paragraph is about string comparison and has nothing to do with binary search. Indeed with that massive if-else statement you are performing linear search.

I don't know if your task is to implement binary search by yourself or just organize your data. In second case I suggest using TreeMap.

EDIT:

class Account implements Comparable<Account> {
    [...]

    @Override
    public int compareTo(Account another) {
        return name.compareTo(another.getName());
    }
}

Now in your code you can:

Arrays.sort(accountArray);
int i = Arrays.binarySearch(accountArray, new Account(name, 0, 0, 0));  
Koikos
  • 162
  • 9
  • What I am trying to do is select an account from the array so that in the next part I can start doing things like withdrawals and deposits and balance checking. Either linear search or binary search it necesary for this project. – TheConfusedMeteor Jun 12 '16 at 06:41
  • @TheConfusedMeteor Well, first of all binary search should be performed on sorted data. As stated in Javadoc for [Arrays.binaruSearch](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#binarySearch-byte:A-byte-) you are using, before use you should call _Arrays.sort_ on that array. Anyway, maybe instead of massive if-else you should use `Arrays.binarySearch(accountArray, n);` – Koikos Jun 12 '16 at 07:06
  • Hmm, although it does make it much shorter and more manageble, I am still getting the error: "Exception in thread "main" java.lang.ClassCastException: Account cannot be cast to java.lang.Comparable at java.util.Arrays.binarySearch0(Unknown Source) at java.util.Arrays.binarySearch(Unknown Source) at Assignment4.main(Assignment4.java:38)" – TheConfusedMeteor Jun 12 '16 at 07:44
  • @TheConfusedMeteor Can you provide more info on error? – Koikos Jun 12 '16 at 07:47
  • "Account cannot be cast to java.lang.Comparable" It is displayed after I enter the input, and it happens whether I use the if-else or the simple binarySearch line. – TheConfusedMeteor Jun 12 '16 at 07:56
  • @TheConfusedMeteor Well, by calling `Arrays.binarySearch(accountArray, "Anonymous");` you are searching in array for string not for account containing that string. Let me think for a moment how to solve it. – Koikos Jun 12 '16 at 08:00
  • In addition to above comment: - Account need to implement Comparable so you can compare Account by their name. It _compareTo_ function is used by Arrays.sort.: @Override public int compareTo(Account another) { return name.compareTo(another.getName()); } - then use binary search like this: Arrays.sort(accountArray); int i = Arrays.binarySearch(accountArray, new Account(name, 0,0,0)); Well, I don't know how to insert code in comment so it's unreadable. – Koikos Jun 12 '16 at 08:37
  • Yeah, you would have to edit your main answer to put it into code form. – TheConfusedMeteor Jun 12 '16 at 08:44