Convert structs in C++ to class in Java [closed]

Java does not do structs. Instead you would create a class. You also need to start using String instead of char[], Java is strongly object oriented and is not very pretty when you try to use procedural style code in it. You also lose a lot of functionality trying to do it that way.

public class Phone {
    private String name;
    private String num;

    public Phone() { }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public String getNum() { return num; }

    public void setNum(String num) { this.num = num; }
}

public class SomeClass {
    public static void main(String[] args) {
        Phone[] book = new Phone[100];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 100; i++) {
            book[i] = new Phone();
            book[i].setName(scanner.next());
            book[i].setNum(scanner.next());
        }
    }
}

Leave a Comment