Question

In: Computer Science

Write Java program Lab52.java which reads in a line of text from the user. The text...

Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method:

  • public static String[] divideText(String input)

The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string.

The program should then print out the returned strings.

Solutions

Expert Solution

Explanation:

Here is the code which has the method divideText which takes the string as input and then all the odd index characters are put together in one string and the even index characters are put together in another string.

Then forming an array of these strings, it is returned to main method.

Code:

import java.util.Scanner;

public class Main
{
public static String[] divideText(String input)
{
String even_str = "", odd_str = "";
  
for(int i=0; i<input.length(); i++)
{
if(i%2==0)
even_str = even_str + input.charAt(i);
else
odd_str = odd_str + input.charAt(i);
}
  
String arr[] = {even_str, odd_str};
  
return arr;
}
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
      
       String line = sc.nextLine();
      
       String arr[] = divideText(line);
      
       System.out.println(arr[0]);
       System.out.println(arr[1]);
   }
}

output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

Write a program that reads a line of text input by the user and places each...
Write a program that reads a line of text input by the user and places each word in a TreeSet. Print the elements of the TreeSet to the screen. This will cause the elements to be printed in ascending order. Using Eclipse for this. TreeSetUse.java.
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Please write a JAVA program which reads a number n from the user and check whether...
Please write a JAVA program which reads a number n from the user and check whether n is a perfect number or not. For example, when n = 7, the print out should be 7 is not a perfect number. If the input n is 6, then the program prints out 6 = 1 * 2 * 3
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Write a Java program that reads an input graph data from a user. Then, it should...
Write a Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2 0...
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Write a Java program (name it PasswordTest) that reads from the user a string input (representing...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing a password) and determines whether the password is “Valid Password” or “Invalid Password”. A valid password has at least 7 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order to...
Write a program named StringWorks.java that asks the user to input a line of text from...
Write a program named StringWorks.java that asks the user to input a line of text from the keyboard.   Ask the user if they want their answers case sensitive or not. You output should be the list of words in the sentence including duplicates A sorted list of the words (alphabetically) A sorted list of words listed backwards (where z comes before a) A randomly shuffled list of works the list of words in the sentence alphabetically removing duplicates. You need...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT