1

I'm making a phone class and a phone book class right now. The phone class declares name and phone number fields, and the phone book class has read and run methods. When run() method is executed in the main function, information is input through the read() method, and information is retrieved and output through the run() method. However, I declared an instance array called phonebooks and tried to input the information received from the read() method into the array, but when I tried to output it through the run method, the array instances were not saved properly. There seems to be an issue where something isn't saving properly, how do I fix it?

import java.util.Scanner;
class Phone {
    static private String name;
    static private String tel;
    public String getNum() {
        return tel;
    }
    public String getName() {
        return name;
    }

    public Phone(String name, String tel) {
        this.name = name;
        this.tel = tel;

    }
}

public class PhoneBook {
    private Scanner scanner;
    static int repeat;
    static Phone[] phonebooks;
    public PhoneBook() {
        scanner = new Scanner(System.in);
    }

    void read() {
        System.out.print("Number of person to store? >> ");
        int repeat = scanner.nextInt();
        Phone[] phonebooks = new Phone[repeat];
        for (int i = 0; i < repeat; i++) {
            System.out.print("Name and Tel. No. >> ");
            String name = scanner.next();
            String tel = scanner.next();
            phonebooks[i] = new Phone(name, tel);
            if (i == repeat - 1) break;
        }
        System.out.println("Saved.");
    }

    void run() {
        read();
        while (true) {
            System.out.print("Who do you wanna search for? >> ");
            String search = scanner.next();
            if (search.equals("stop")) break;
            int i;
            for (i = 0; i < repeat; i++) {
                if (phonebooks[i].getName() == search)
                    System.out.println(search + "'s number is " + phonebooks[i].getNum());
                break;
            }
        }
        scanner.close();
    }


    public static void main(String[] args) {
        new PhoneBook().run();
    }
}
AlessoFan
  • 13
  • 3
  • 1
    The `Phone[] phonebooks` in your `read()` method is a local variable and therefore has nothing to do with your class level `phonebooks`. In your `read()`, change the variable declaration to `phonebooks = new Phone[repeat]`. Also, there is no point in your class variables being `static` in my opinion. – maloomeister Apr 11 '22 at 10:58
  • Another sidenote, because of `if (phonebooks[i].getName() == search)` -> [How do I compare Strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Apr 11 '22 at 11:01
  • Your naming is confusing. What you call 'Phone' should probably be `Contact`. `Phonebook` would encapsulate a collection (or an array) of `Contact`. The whole app (main class) might be called `Phone` – g00se Apr 11 '22 at 11:01

1 Answers1

0

There are few issues in your code, below are some suggestions to make it work--

  1. change member variables from static private to only private in Phone

  2. Don't redeclare repeat & phonebooks in read().

  3. Change if (phonebooks[i].getName() == search) call in run() to if (phonebooks[i].getName().equalsIgnoreCase(search)) so that it will search & match search string.

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36