Question

Java Programming I need to create an application that have an Arraylist string with 6 elements....

Java Programming

I need to create an application that have an Arraylist string with 6 elements. Then it ask the user to input more elements until it reaches 10 elements in total.

After that the application remove 1 element with remove() by index.

To finalize it uses a for-each loop to display the list.

Thanks for your help!

Homework Answers

Answer #1
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

import java.util.ArrayList;
import java.util.Scanner;

public class List {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        ArrayList<String> elements = new ArrayList<>();
        //Arraylist string with 6 elements.
        elements.add("Brazil");
        elements.add("China");
        elements.add("India");
        elements.add("Spain");
        elements.add("Germany");
        elements.add("Singapore");

        // Add 4 more elements ;
        // it ask the user to input more elements until it
        // reaches 10 elements in total.
        for(int count=1; count<=4; count++){
            System.out.print("Enter a string: ");
            String str = scanner.nextLine();
            elements.add(str);
        }

        System.out.println("ArrayList Before Deleting: ");
        for(int i=0;i<elements.size();i++){
            System.out.println(i+": "+elements.get(i));
        }

        //After that the application remove 1 element with remove() by index.
        System.out.print("Enter an index [0-9]: ");
        int index = scanner.nextInt();

        if(0<=index && index<elements.size()){
            elements.remove(index);

            System.out.println("\nArrayList after Deleting");
            //To finalize it uses a for-each loop to display the list.
            for(int i=0;i<elements.size();i++){
                System.out.println(i+": "+elements.get(i));
            }
        }else{
            System.out.println("Error: Index out of range.");
        }



    }
}

=============================================================

Know the answer?
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
Similar Questions
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering “Scanner in = new Scanner (System.in);”, input a value into element 5 of one-dimensional double array nums. c) Initialize each of the four elements of the one-dimensional integer array test to 7. d) Declare and create an array called table as a float array that has four rows and three columns. e) Considering the following ArrayList declaration, insert “test” at the fourth position (index...
I have the below homework problem for a python programming class, and i really need some...
I have the below homework problem for a python programming class, and i really need some help!! First, take a set of 6 grades from a user and average them. Provide the average to the user. You need to check to make sure the grades are within the normal range. If the grade is less than 0 or more than 100, issue a warning to the user. You don't need to take the grade again, just let the user know....
java programming 1.Create a simple SWING Form to Display Student Data.The top part of the form...
java programming 1.Create a simple SWING Form to Display Student Data.The top part of the form should say “Student Information”.The middle part of the form will contain labels and textFields; one each for ID, FirstName, LastName, Email and GPA.The bottom part should have 5 Buttons that read “Find”, “Insert”, “Delete”, “Update” and “Exit”. 2.Add a few neat SWING features, like tooltips, borders, colors, etc. Also the Exit button should work and the “X” at the top right of the Window...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
C# Programming Using the Example provided in this Week Folder,Create a Console application that solve the...
C# Programming Using the Example provided in this Week Folder,Create a Console application that solve the following problem: The heating system in a school should be switched on if the average temperature is less than 17 degrees Celsius. The average temperature is found from the temperatures in the Math, English and IT departments. You are required to write a program that allows the user to input 3 temperatures. The program calculates and displays the average temperature and then displays "heating...
I need the java code for a 4-function calculator app on android studio. Please make sure...
I need the java code for a 4-function calculator app on android studio. Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers). The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Create a very simple TCP Application in JAVA. You will create a client and a server....
Create a very simple TCP Application in JAVA. You will create a client and a server. The client must execute after you start the server. Here is what they will do. The client will read from the console a string that the users enters. This string will be the name of the user, let's say Bob. The client then sends the name, Bob, in an Object Stream, to the server. The server will receive this Object Stream and send to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT