Using a scanner to accept String input and storing in a String Array

113,831

Solution 1

A cleaner approach would be to create a Person object that contains contactName, contactPhone, etc. Then, use an ArrayList rather then an array to add the new objects. Create a loop that accepts all the fields for each `Person:

while (!done) {
   Person person = new Person();
   String name = input.nextLine();
   person.setContactName(name);
   ...

   myPersonList.add(person);
}

Using the list will remove the need for array bounds checking.

Solution 2

One of the problem with this code is here :

name += contactName[];

This instruction won't insert anything in the array. Instead it will concatenate the current value of the variable name with the string representation of the contactName array.

Instead use this:

contactName[index] = name;

this instruction will store the variable name in the contactName array at the index index.

The second problem you have is that you don't have the variable index.

What you can do is a loop with 12 iterations to fill all your arrays. (and index will be your iteration variable)

Solution 3

//go through this code I have made several changes in it//

import java.util.Scanner;

public class addContact {
public static void main(String [] args){

//declare arrays
String [] contactName = new String [12];
String [] contactPhone = new String [12];
String [] contactAdd1 = new String [12];
String [] contactAdd2 = new String [12];
int i=0;
String name = "0";
String phone = "0";
String add1 = "0";
String add2 = "0";
//method of taken input
Scanner input = new Scanner(System.in);

//while name field is empty display prompt etc.
while (i<11)
{
    i++;
System.out.println("Enter contacts name: "+ i);
name = input.nextLine();
name += contactName[i];
}


while (i<12)
{
    i++;
System.out.println("Enter contacts addressline1:");
add1 = input.nextLine();
add1 += contactAdd1[i];
}

while (i<12)
{
    i++;
System.out.println("Enter contacts addressline2:");
add2 = input.nextLine();
add2 += contactAdd2[i];
}

while (i<12)
{
    i++;
System.out.println("Enter contact phone number: ");
phone = input.nextLine();
phone += contactPhone[i];
}

}   
}
Share:
113,831
Rachael
Author by

Rachael

Updated on July 09, 2022

Comments

  • Rachael
    Rachael almost 2 years

    Can someone help me please. I have done numerous searches but can't find a solution anywhere. I'm a beginner to Java and currently practicing some code while on a break from college.

    I am trying to make a Phonebook program. At the moment I am trying to add a new contact, below is the code i have but i am not sure how to store the information in an array can someone give me some pointers please.

        import java.util.Scanner;
    
        public class addContact {
        public static void main(String [] args){
    
        //declare arrays
            String [] contactName = new String [12];
            String [] contactPhone = new String [12];
            String [] contactAdd1 = new String [12];
            String [] contactAdd2 = new String [12];
    
        //inputs
        String name = "";
        String phone = "";
        String add1 = "";
        String add2 = "";
    
        //method of taken input
        Scanner input = new Scanner(System.in);
    
        //while name field is empty display prompt etc.
        while (name.equals(""))
        {
        System.out.println("Enter contacts name: ");
        name = input.nextLine();
        name += contactName[];
        }
    
    
        while (add1.equals(""))
        {
        System.out.println("Enter contacts addressline1:");
        add1 = input.nextLine();
        add1 += contactAdd1[];
        }
    
        while (add2.equals(""))
        {
        System.out.println("Enter contacts addressline2:");
        add2 = input.nextLine();
        add2 += contactAdd2[];
        }
    
        while (phone.equals(""))
        {
        System.out.println("Enter contact phone number: ");
        phone = input.nextLine();
        phone += contactPhone[];
        }
    
        }   
    }