Hello sir's/ma'am ,i'm new to Arrays and i got an assignment to do but i'am having a problem in arrays and how to input a String...

here's my code.

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

class AddressBook
{
	public static void main(String[]args)
	{
		
		Scanner in = new Scanner(System.in);
		Scanner ch = new Scanner(System.in);
		

		String[] name = new String[1024];
		int ctr = 0;
		int choice;
		char choice2;
		String exit = " Thank you for using my program ";
		
		
				String menu = " WELCOME TO JOSEPH JENAIRE PUMARAS ADDRESS BOOK";

				System.out.printf("\t\t%s",menu);
			do{
				
				System.out.print("\n[1]Add Person");
				System.out.print("\n[2]Report ");
				System.out.print("\n[3]Exit ");
				System.out.print("\nEnter your choice :");
				choice = in.nextInt();



			
			switch(choice)
			{
				case 1:
					 System.out.print("Enter Name: ");
					  name = in.next();
					 
					break;

				case 2:

					for (int i = 0 ; i > ctr ;i++)
					{
						System.out.println(name[i]);
						
					}
						break;
			}
			

				System.out.print("Would you like to add again? [Y]Yes / [N]No :");
				choice2 = ch.next().charAt(0);

				}while(choice != 3);
				

	}


}

The report choice in my main menu is supposed to be for displaying all the names that has been entered.... please help me..

First of all you need to use only one Scanner. You don't need two.

Remove the choice2. You don't need it. When you do choice = in.nextInt and you choose 3, then the check you put at the while will make your loop exit.

And most importantly, you read the name but you don't add it to the array. Also when you loop the array you need i<ctr. And every time you add a name at the array increase the ctr. The first time the array is empty, every time you add a name increase the ctr, so the loop will print only up to the number of names entered.

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

class AddressBook
{
	public static void main(String[]args)
	{
		
		Scanner in = new Scanner(System.in);

		String[] name = new String[1024];
		int ctr = 0;
		int choice;
		String exit = " Thank you for using my program ";
		
		
				String menu = " WELCOME TO JOSEPH JENAIRE PUMARAS ADDRESS BOOK";

				System.out.printf("\t\t%s",menu);
				System.out.println();
			do{
				
				System.out.println("[1]Add Person");
				System.out.println("[2]Report ");
				System.out.println("[3]Exit ");
				System.out.println("Enter your choice :");
				choice = in.nextInt();



			
			switch(choice)
			{
				case 1:
					 System.out.print("Enter Name: ");
					  name[ctr] = in.next();
  					  ctr++;

					break;

				case 2:

					for (int i = 0 ; i < ctr ;i++)
					{
						System.out.println(name[i]);
						
					}
						break;
			}
			

				}while(choice != 3);
				

	}


}

At first the ctr is 0. So the first name should be put at name[0] (name[ctr]). Then you increase the ctr. ctr is now 1. So the next time you call: name[ctr] = in.next the ctr is 1 and will be put at the 1 position. Then ctr is increased. So every time you try to add a name you put it in the name array, and the ctr is increased so the next name will be put at the next position.

And another more user friendly version:

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

class AddressBook
{
	public static void main(String[]args)
	{
		
		Scanner in = new Scanner(System.in);

		String[] name = new String[1024];
		int ctr = 0;
		int choice;
		String exit = " Thank you for using my program ";
		
		
				String menu = " WELCOME TO JOSEPH JENAIRE PUMARAS ADDRESS BOOK";

				System.out.printf("\t\t%s",menu);
				System.out.println();
			do{
				
				System.out.println("[1]Add Person");
				System.out.println("[2]Report ");
				System.out.println("[3]Exit ");
				System.out.println("Enter your choice :");
				choice = in.nextInt();



			
			switch(choice)
			{
				case 1:
					 System.out.print("Enter Name: ");
					  name[ctr] = in.next();
  					  ctr++;

					break;

				case 2:

					for (int i = 0 ; i < ctr ;i++)
					{
						System.out.println(name[i]);
						
					}
						break;
                                case 3:
                                   System.out.println("Will exit");
                                   break;
                                default:
                                   System.out.println("Wrong choice");
                                   break;
			}
			

				}while(choice != 3);
				

	}


}

thank you so much sir :*

I got your problem you are getting only one name and try to display many how it is possible

//initializing
int i=0;
String name[]=new String[50];

For adding case 1:

            System.out.print("Enter Name: ");

            name[i] = in.next();

            i++;

this will store until you press other key values

To display or print case2:

         for (int j = 0 ; j < i ;j++)
         {
               System.out.println(name[j]);
         } 

This will traverse in your array and will print all the names

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.