237,563 views
40 votes
40 votes
I am trying to write a class that asks the user to input their address. Once the address is entered the program will find all the letters “a”, “e” and “o” after cycling them through for a loop. If any of these letters exist, it will take them, store them in an array list, and print them out.

User Sebastian Juarez
by
2.6k points

1 Answer

15 votes
15 votes

Answer:

Here is an example of how you could write a class that asks the user to input their address, and then finds and stores any instances of the letters "a", "e", and "o" in an array list:

import java.util.ArrayList;

import java.util.Scanner;

public class Address {

public static void main(String[] args) {

// Create a Scanner object to read input from the user

Scanner input = new Scanner(System.in);

// Prompt the user to enter their address

System.out.print("Enter your address: ");

String address = input.nextLine();

// Create an array list to store the letters "a", "e", and "o"

ArrayList<Character> vowels = new ArrayList<>();

// Cycle through the address string, looking for the letters "a", "e", and "o"

for (int i = 0; i < address.length(); i++) {

char c = address.charAt(i);

if (c == 'a' || c == 'e' || c == 'o') {

// If one of the letters is found, add it to the array list

vowels.add(c);

}

}

// Print out the array list of vowels

System.out.println("Vowels found in your address: " + vowels);

}

}

Step-by-step explanation:

This code uses a for loop to cycle through each character in the address string, and checks if it is one of the letters "a", "e", or "o". If it is, it adds the character to the array list. Finally, it prints out the array list of vowels.

I hope this helps! Let me know if you have any questions.

User PerseP
by
3.0k points