简体   繁体   中英

How to set Instance Variables to user-inputted value

I am creating a program that uses an array to hold references to multiple Objects of type Contact. I would like to use user input to allow the user to set the name and phoneNumbber values of a newly created Contact object. how would I go about doing this?

import java.io.* ;
import java.util.*;

public class Contact {

public static int count ;
public String name ;
public int phoneNumber ;


public Contact(String name , int phoneNumber) {

  count++ ;
  this.name = name ;
  this.phoneNumber = phoneNumber ;

  }

  public String getName() {

     return this.name;
  }

  public int getNumber() {

     return this.phoneNumber;

  }


  public static void main (String args[]) {

  Scanner input = new Scanner(System.in);
  Contact[] list = new Contact[4] ;

  list[0] = new Contact("Philly",5550) ;
  list[1] = new Contact("Becky",6330) ;
  list[2] = new Contact("Rufio",4456) ;

  System.out.println("There are " + count + " contacts");

  for( int i = 0 ; i<3 ; i++ ) {
  System.out.println(list[i].getName()) ;
  System.out.println(list[i].getNumber()) ;
  System.out.println("---") ;

  System.out.println("Would you like to add another? Yes/No");
  String answer = input.next() ;

     if( answer = "No" ) {
        System.out.println("Goodbye.") ;
        }
     else {
        System.out.println(" Sure, what is the new contacts name?");
        String newName = input.next();
        newContact.name = newName ;

        System.out.println("and the number?");
        int newNumber = input.nextInt();
        newContact.phoneNumber = newNumber ;
        }

     Contact newContact = new Contact() ;



     list[3] = newContact ;


     for( int j = 0 ; j<=3 ; j++ ) {
  System.out.println(list[j].getName()) ;
  System.out.println(list[j].getNumber()) ;
  System.out.println("---") ;
     }

}
}
}

Typically if you want to create a new object with certain values, you use the constructor method. I would start by moving your main method to a separate class, then using a code such as:

     Scanner input = new Scanner(System.in);
     System.out.println("Enter the name of the contact");
     String name = input.next();
     System.out.println("Enter the phone number");
     int num = input.nextInt();
     list[3] = new Contact(name, num);

I would suggest using an ArrayList instead of an array because the ArrayList can handle more entries and expand as needed so you don't have to worry about the exact array size when adding elements.

This will do it...I would also recommend changing your members to be private to use proper encapsulation and since you already created getters, there's no need to have the members public. If at any point you needed to set the values outside of the constructor, create setters for the private member variables.

if( answer.equals("No") ) { // Need to use .equals instead of = in Java
   System.out.println("Goodbye.") ;
} else {
    System.out.println(" Sure, what is the new contacts name?");
    String newName = input.next();

    System.out.println("and the number?");
    int newNumber = input.nextInt();

    Contact newContact = new Contact(newName, newNumber) ;
    list[3] = newContact ;
}

for( int j = 0 ; j<=3 ; j++ ) {
    if(list[j] != null) { // because you're using a fixed number, make sure the value isn't null
       System.out.println(list[j].getName()) ;
       System.out.println(list[j].getNumber()) ;
       System.out.println("---") ;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM