java - I am having issues with a basic contact manager that I am creating. -
i having problems bit of code. first thing did created arrays, these arrays hold information each contact, building basic contact manager. main method calls menu method start sequence. @ menu, user has choice of want do. whichever choice choose determined number enter on keyboard. in turn, activate different method.
the problems having follows:
after pressing "1" view contacts, computer either spits out 100 nulls or 100 repeats of whatever last inputted in "2", add contacts.
although yes want menu repeat after action has taken place, instantaneously. example, repeats happen after push "1" goes straight main menu, , difficult read way.
import java.io.ioexception; import java.util.scanner; ``public class mainhub { // global variables needed static string[] name = new string[100]; static string[] number = new string[100]; static string[] email = new string[100]; static string[] address = new string[100]; static string[] birthday = new string[100]; static string[] nickname = new string[100]; static int x; public static int counter = 0; static scanner in = new scanner(system.in); public static void main (string[] args) throws ioexception{ menu(); // call menu method start program } //holds information menu public static void menu() throws ioexception{ int choice = -1; //start @ -1 not compete index number while (choice != 7){ //while loop choice user //telling user is, simple output message system.out.println("\t main menu\n"); system.out.println("enter corrosponding number want do.\n"); system.out.println("1.view contacts\n2.add contact\n3.edit contact\n4.delete contact\n5.save contact list\n6.load contact list\n7.exit"); choice = integer.valueof(in.nextline()).intvalue(); //this allows user input read , used make choice switch(choice){ case 1: viewallcontacts(); //if user inputs 1, call method view contacts break; //stop loop case 2: addcontact(); //if user inputs 2, call method add contact break; //stop loop case 3: editcontact(); // if user inputs 3, call method view contacts , choose 1 edit break; //stop loop case 4: deletecontact(); // if user inputs 4, call method view contacts , choose 1 delete break; //stop loop case 5: savecontact(); // if user inputs 5, call method save current contact list text file break; //stop loop case 6: loadcontact(); // if user inputs 6, call method load text file input contacts array break; //stop loop case 7: system.out.println("you exiting"); // if user inputs 7, tell user leaving break; //stops loop default: system.out.println("that not 1 of options."); // if user doesn't input 1 of above numbers, tell user not option } } } //holds information, once called upon user able see contacts public static void viewallcontacts(){ while(counter<100){ //while counter has less 101 print out full list of contacts if(counter>0){ //if counter greater 0 print list of contacts system.out.println("full name: " +name[x]); system.out.println("number: " +number[x]); system.out.println("e-mail address: " +email[x]); system.out.println("home address: " +address[x]); system.out.println("birthday: " +birthday[x]); system.out.println("nickname: " +nickname[x]); system.out.println(" "); //space way contact list bit prettier counter++; }else{ system.out.println("there no contacts in list yet."); //else tell user there no contacts } } } //lets user add contact , information public static void addcontact() throws ioexception{ //as long counter less 101 can add contacts if(counter<100){ system.out.println("enter contact's full name: "); //allows user add name contact name[x] = in.nextline(); //whatever typed added name variable system.out.println("enter contact's number: "); //allows user add number contact number[x] = in.nextline(); //whatever typed added number variable system.out.println("enter contact's e-mail address: "); //allows user add e-mail address contact email[x] = in.nextline(); //whatever typed added email variable system.out.println("enter contact's home address: "); //allows user add home address contact address[x] = in.nextline(); //whatever typed added address variable system.out.println("enter contact's birthday: "); //allows user add birthday contact birthday[x] = in.nextline(); //whatever typed added birthday variable system.out.println("enter contact's nickname: "); //allows user add nickname contact nickname[x] = in.nextline(); //whatever typed added nickname variable counter++; //adds 1 counter allow space next contact in arrays }else{ system.out.println("sorry, contact list full."); //if counter 101 print contact list full } system.out.println("your contact has been added"); }
the problem using x
index of array, x
never being incremented.
as incrementing counter
try
system.out.println("full name: " +name[counter]);
likewise adding contact
sort out indexes.
Comments
Post a Comment