I need to insert a Character such as a at multiples of a var

I need to insert a Character, such as a, at multiples of a variable into a java, string literal. For example, if the string litreal is: Hello world and the variable z is 2. The program will need to insert \'a\' into the string literal at multiples of 2. So at positions 2,4,6,8,10 and so on until the end of the literal.

The new literal should = Heallao waoralda.

This is my program this far:

import java.util.Random; // Imports Random class to be used later.
import java.util.Scanner; // Imports scanner class to allow use later.


public class Message // Class declaration.
{
   static Random rand = new Random(); // Creates new object of class Random.
  
   private char x1;
   private char x2;
   private char x3;
   private char x4;
   private char y1;
   private char y2;
   private int z;
   private String key;
   private StringBuilder temp;
  

  
   private void set_Key() // Class message constructor.
   {
       x1 = (char)(rand.nextInt(26) + \'a\');
       x2 = (char)(rand.nextInt(26) + \'a\');
       x3 = (char)(rand.nextInt(26) + \'a\');
       x4 = (char)(rand.nextInt(26) + \'a\');
       y1 = (char)(rand.nextInt(13) + \'!\');
       y2 = (char)(rand.nextInt(13) + \'!\');
       z = 2;

   }
  
   private void generate_Key() // Method to generate the key.
   {
       // Appends all the variables together in specified format and sets them = to key.
       key = new StringBuilder().append(x1).append(x2).append(\"-\").append(y1).append(x3).append(x4).append(\"-\").append(z).append(y2).toString();
      
   }
  
   private void print_Key() // Method to print secret key.
   {
       System.out.println(key); // Prints key.
   }
  
   public void encrypt()
   {
       Scanner read=new Scanner(System.in); // Creates object of scanner class.
String input=\"\";
String output=\"\";
  
//prompt the user for input.
input=read.nextLine();
//split input based on spaces
String str[]=input.split(\" \");

//iterate through each loop and fetch first character and append at last
for(int i=0;i<str.length;i++)
{
   if(str[i].charAt(0) == \'a\' || str[i].charAt(0) == \'e\' || str[i].charAt(0) == \'i\' || str[i].charAt(0) == \'o\' || str[i].charAt(0) == \'u\')
   {
       output += str[i] + y1 + x3 + x4 + \" \";
      
   }
   else
output+= str[i].substring(1)+str[i].charAt(0)+ x1 + x2 + \" \";
}
  
System.out.println(output);
read.close();
}

  
  
  
   public static void main(String [] args) // Main method.
   {
   Scanner input = new Scanner(System.in); // New scanner class object for console input.
       Message message = new Message();
       message.set_Key();
       message.generate_Key();
       message.print_Key();
      
       System.out.println(\"Enter your message to be encrypted.\");
       message.encrypt();
  
      
      
       input.close(); // closes scanner class object
      
   }
}

Solution

// Length

int length() // returns the length of the String

boolean isEmpty() // same as thisString.length == 0

// Comparison

boolean equals(String another) // CANNOT use \'==\' or \'!=\' to compare two Strings in Java

boolean equalsIgnoreCase(String another)

int compareTo(String another) // return 0 if this string is the same as another;

// <0 if lexicographically less than another; or >0

int compareToIgnoreCase(String another)

boolean startsWith(String another)

boolean startsWith(String another, int fromIndex) // search begins at fromIndex

boolean endsWith(String another)

// Searching & Indexing

int indexOf(String search)

int indexOf(String search, int fromIndex)

int indexOf(int character)

int indexOf(int character, int fromIndex) // search forward starting at fromIndex

int lastIndexOf(String search)

int lastIndexOf(String search, int fromIndex) // search backward starting at fromIndex

int lastIndexOf(int character)

int lastIndexOf(int character, int fromIndex)

// Extracting a char or part of the String (substring)

char charAt(int index) // index from 0 to String\'s length - 1

String substring(int fromIndex)

String substring(int fromIndex, int endIndex) // exclude endIndex

// Creating a new String or char[] from the original (Strings are immutable!)

String toLowerCase()

String toUpperCase()

String trim() // create a new String removing white spaces from front and back

String replace(char oldChar, char newChar) // create a new String with oldChar replaced by newChar

String concat(String another) // same as thisString + another

char[] toCharArray() // create a char[] from this string

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) // copy into dst char[]

// Static methods for converting primitives to String

static String ValueOf(type arg) // type can be primitives or char[]

// Static method resulted in a formatted String using format specifiers

static String format(String formattingString, Object... args) // same as printf()

// Regular Expression

boolean matches(String regexe)

String replaceAll(String regexe, String replacement)

String replaceAll(String regexe, String replacement)

String[] split(String regexe) // Split the String using regexe as delimiter,

// return a String array

String[] split(String regexe, int count) // for count times only

I need to insert a Character, such as a, at multiples of a variable into a java, string literal. For example, if the string litreal is: Hello world and the vari
I need to insert a Character, such as a, at multiples of a variable into a java, string literal. For example, if the string litreal is: Hello world and the vari
I need to insert a Character, such as a, at multiples of a variable into a java, string literal. For example, if the string litreal is: Hello world and the vari

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site