Create a program using a Java console application name it "Alphabetizer". The program should ask the user to enter is name and surname.
Create a method called emptySpace () this remove space between the name and the surname (Use a regular expression "replaceAll"). The emptySpace() method also Count the number of characters within the newly created string (nameSurname) and return the total number of Characters .
The total number of characters (Name and Surname) should be the size of your arrayAlphabetizer (type integer). Populate your arrayAlphabetizer with a series of random numbers between 10 and 50.
Display all arrayAlphabetizer elements and they are corresponding indexes before executing the sort() method and after executing the sort(). Allow the user to enter a value from the arrayAlphabetizer element to search for.
Loop through the array until you find the value and replace that value with the character @.
Print out arrayAlphabetizer with the replaced element..
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static int emptySpace(String nameSurname){
nameSurname = nameSurname.replaceAll(" ", "");
return nameSurname.length();
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
String nameSurname= in.nextLine();
Random rand = new Random();
String[] arrayAlphabetizer = new String[emptySpace(nameSurname)];
for (int i = 0; i < arrayAlphabetizer.length; i++) {
arrayAlphabetizer[i] = "" + rand.nextInt(10, 51);
}
System.out.println(Arrays.toString(arrayAlphabetizer));
String ch = in.next();
for (int i = 0; i < arrayAlphabetizer.length; i++) {
if(ch.equals(arrayAlphabetizer[i])){
arrayAlphabetizer[i] = "@";
}
}
System.out.println(Arrays.toString(arrayAlphabetizer));
}
}
Comments
Leave a comment