Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a project that asks the user to enter a String . Once you have that Strin

ID: 3599187 • Letter: C

Question

Create a project that asks the user to enter a String. Once you have that String value, create an array of characters that has the same size as the entered String. Loop through the String and save each individual character into each element of the created array.

For example: If the user enters hello world the array would be of size 11 (it would have 11 elements):

Array:

Notice how even the space is counted as an individual element. That's because it counts as an individual character in the String.

After the array has been created, you will be displaying it back to the user in two ways:

1. Use a for loop to iterate through and display each element of the character array.
2. Use the following code snippet: System.out.println(Arrays.toString(arrayName));
For this method, replace arrayName with the actual name of your character array. You will also need to import java.util.Arrays;

Don't forget to test your program throughout the coding process!

Repeat this process by allowing the user to keep entering new Strings, until they enter "exit" to quit. Once the user enters exit, stop iterating through the loop and display a goodbye message.

make sure to use java

Index 0 1 2 3 4 5 6 7 8 9 10 Element 'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd'

Explanation / Answer

//The code is given below with required comments.

import java.util.Scanner;
import java.util.Arrays;
class ArrayAndString{
public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  //Creating an empty string
  String inputString = "";

  while(true){
   // Taking input using Scanner Class instance
   inputString = in.nextLine();
   if( inputString.equals("exit")) break;
   
   // Creating an array of same size as the string
   char[] charArray = new char[inputString.length()];

   //Iterating and add all the characters to the char array
   for( int i = 0 ; i < inputString.length() ; i++ ){
    charArray[i] = inputString.charAt(i);
   }

   //Printing char elements one by one using for each iteration loop
   for(char ch : charArray){
    System.out.print(ch);
   }
   
   //Printing char array using Arrays.toString() method
   System.out.println(" " + Arrays.toString(charArray));
  }

  System.out.println("Goodbye");
  //Closing the input stream
  in.close();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote