0

My customer.dat files looks like as follows:

 4
 Jon 1000
 Jane 2000
 Jake 20000
 Drake 100000

The first number represents the total number of customers in that file, while the first string is the Name of the customer and the latter is the balance they have.

The following code is the application code I have written. I want it to read from the customer.dat and create an array. The problem with the application is that I created a customer ID, which checks the name the user inputs(if it exists in the array( and then sets a customer ID. However, whenever I run the application, no matter what name I type, even if I type Drake - and type "1" to view the portfolio, it ends up always showing the first name which is Jon.

    Scanner infile2 = new Scanner(new File("customer.dat"));

    customer[]mycustomers; // declaring an array to stock

    int numofcustomers = infile2.nextInt();
    mycustomers = new customer [numofcustomers]; 

    for (int i = 0; i < numofcustomers; i++)
    {
        mycustomers[i] = new customer(infile2.next(),infile2.nextDouble(), numofcustomers);
        System.out.println(mycustomers[i].toString());
    }
    Scanner input = new Scanner (System.in);
    System.out.println("Please type in your name");
    String Name  = input.nextLine();
    int cusID = 0;
    for (int i = 0; i < numofcustomers; i++) 
    { 
       if ( Name == mycustomers[i].getName())
       {
           cusID = i;

       }
    }
    System.out.println("Please type in 1 for a customer to view their portfolio, 2 to trade stocks or 3 to exit the application");   
    int choice  = Integer.parseInt(input.nextLine());

    if (choice == 1)
    {
            System.out.println(mycustomers[cusID].toString());

    }

The following is a snippet of what is located in my customer.java which contains setters and getters:

    public customer(String n, double b, int Size)
    {
        Name = n;
        balance = b;
        stock[] A = new stock[Size];
        Portfolio = A;
    }

    public String getName() {
        return Name;
    }

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

I'm not sure if I was able to describe the problem correctly, but if not, let me know if any more information is required.

1 Answers1

0

First, Java naming conventions usually dictate you CamelCase class names (i.e. rename to Customer instead of customer).

I believe your problem is here:

Name == mycustomers[i].getName()

In Java, Strings are objects, and you have to use the equals() method to do a comparison, like so:

Name.equals(mycustomers[i].getName())

If you use the standard ==, the comparison is done on the object's memory location, so unless Name and getName() are literally the exact same String object (unlikely), that will be false. This means the if statement never evaluates to true, cusID remains 0 as you initialized it, and so you print out the first name in the list, Jon.

I would suggest, in addition to changing the above, that you initialize cusID to -1 so you can differentiate between a name that is not found in the list and the selection of the first element in the list. Also, what if two people by the same name are in the list? (Hint: The first one will never be selected in your current implementation.)