Question

Create a Java program which asks the user how many names they want to enter. The...

Create a Java program which asks the user how many names they want to enter. The program should then instantiate an array that will hold this many names. The main method should call a method, getNames() to allow the user to enter the names. A for loop should be used in this method. Once the names are entered, the main method should call another method displayNames() which will use a while loop to display the names entered.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/**
 * Please find the required solutions
 */

import java.util.Scanner;

public class NamesApp {

    /*Get Names method inputs names of length of parameters*/
    public static void getNames(String[] names) {

        // read names from user using for loop
        Scanner keyboard = new Scanner(System.in);
        for (int i = 0; i < names.length; i++) {
            System.out.println("Enter name " + (i + 1) + ": ");
            names[i] = keyboard.nextLine();
        }
        // finally close resource
        keyboard.close();
    }

    /**
     * Display names using while loop
     *
     * @param names
     */
    public static void displayNames(String[] names) {
        System.out.println("Displaying names: ");
        int i = 0;
        while (i < names.length)
            System.out.println(names[i++]);
    }

    /**
     * Main method with required implementation
     */
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter number of names to enter");
        int numberOfNames = keyboard.nextInt();
        keyboard.nextLine();

        String[] names = new String[numberOfNames];

        getNames(names);

        displayNames(names);
        keyboard.close();
    }
}
Sample output:

Add a comment
Know the answer?
Add Answer to:
Create a Java program which asks the user how many names they want to enter. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ***** JAVA ONLY ***** Write a program that asks the user to enter the name of...

    ***** JAVA ONLY ***** Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad to create a simple file that can be used to test the program. ***** JAVA ONLY *****

  • Write a console-based program ( C # ) that asks a user to enter a number...

    Write a console-based program ( C # ) that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined...

  • Create a program that allows a user to enter up to five names of friends. Use...

    Create a program that allows a user to enter up to five names of friends. Use a twodimensional array to store the friends’ names. After each name is entered, the user should have the option to enter another name or print out a report that shows each name entered thus far

  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • write a program code that asks the user how many numbers they wish to find the...

    write a program code that asks the user how many numbers they wish to find the statistics of and then asks the user to enter those numbers. The program must then calculate and display the average variance and standard deviation of the numbers entered by the user. the program code must -ask three user how many numbers they wish to find the statistics of -ask the user to enter those numbers and store them in an array - use a...

  • Create two java programs. The first will ask the user to enter three integers. Once the...

    Create two java programs. The first will ask the user to enter three integers. Once the three integers are entered, ask the user to enter one of three functions. The “average” if they want the average of the three numbers entered, enter “min” if they want the minimum and “max” if they want the maximum. Display the answer. Ask the user if they want to calculate another function for the three numbers. If so, loop and ask what function they...

  • 5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create...

    5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create a method called generateSuperLottoNumbers() that returns an array of 6 random SuperLotto lottery numbers. The first 5 numbers must be from the range 1 to 47 The 6th number (the MEGA) must be from 1 to 27. Create a method called printTicket() that takes an integer array as an parameter it will loop through the integer array and print out the data Display the...

  • Write C++ program (studentsGpa.cpp) uses dynamic allocation to create an array of strings. It asks the...

    Write C++ program (studentsGpa.cpp) uses dynamic allocation to create an array of strings. It asks the user to enter a number and based on the entered number it allocates the array size. Then based on that number it asks the user that many times to enter student’s names. What you need to do:  Add another array of doubles to store the gpa of each student as you enter them  You need to display both the student’s name and...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • Write a program that first asks the user how many budget items they have. Then using...

    Write a program that first asks the user how many budget items they have. Then using dynamic arrays, create the appropriate arrays to hold the following data: Allow the user to enter the budget items and amount budgeted for the number items created in the dynamic array. (For your testing, just use four or five.) Once all budget items are entered, the program should then output each budget item, the amount of money budgeted for that item, and the percentage...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT