Question

In: Computer Science

Write a program that concatenates the characters in a two-dimensional into a set of Strings in...


Write a program that concatenates the characters in a two-dimensional into a set of Strings in a
one-dimensional array as described below.
main
#Request and Read in the number of rows of a two dimensional array.
#Create the reference to the two-dimensional array.
For each row
#Request and Reads in a line of text
#Based on the length of the line create a row with the correct number of columns (look
#back to lesson 3)
#Place each character for the String into the row, one character at a time.
#Call concatenateColumnsPerRow with a reference to the character two-dimensional array as a
#reference. The return will be a reference to a one-dimensional String array.
#Call displayArray with a reference to the one-dimensional StringArray.
concatenateColumnsPerRow
#Accepts a character two-dimensional array reference as input.
#Concatenates the elements in each row into a String.
#Returns a one-dimensional String array, with the Strings from each row placed into the
#corresponding row in the String array.
displayArray
#Accepts the one-dimensional String array and prints out the Strings on separate lines

Solutions

Expert Solution

import java.util.Scanner;

public class MultiDimensionalArrayConcater {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //Request and Read in the number of rows of a two dimensional array.
        System.out.printf("Enter number of rows: ");
        int rows = scanner.nextInt();

        //Create the reference to the two-dimensional array
        char[][] arr = new char[rows][];

        for (int i = 0; i < rows; i++) {
            //Request and Reads in a line of text
            System.out.printf("Enter line: ");
            String word = scanner.next();
            //Based on the length of the line create a row with the correct number of columns
            arr[i] = new char[word.length()];
            for (int j = 0; j < word.length(); j++) {
                //Place each character for the String into the row, one character at a time
                arr[i][j] = word.charAt(j);
            }
        }
        //Call concatenateColumnsPerRow with a reference to the character two-dimensional array as a
        //reference.
        String[] result = concatenateColumnsPerRow(arr);
        //Call displayArray with a reference to the one-dimensional StringArray
        displayArray(result);
    }

    //Accepts a character two-dimensional array reference as input.Concatenates the elements in each row into a String
    //Returns a one-dimensional String array, with the Strings from each row placed into the corresponding row in the String array
    public static String[] concatenateColumnsPerRow(char[][] arr) {
        String[] result = new String[arr.length];
        for (int i = 0; i < arr.length; i++) {
            String word = "";
            //Creating word of a row
            for (int j = 0; j < arr[i].length; j++) {
                word += (arr[i][j]);
            }
            //adding to result 
            result[i] = word;
        }
        return result;
    }

    //Accepts the one-dimensional String array and prints out the Strings on separate lines
    public static void displayArray(String[] arr) {
        System.out.println();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

Related Solutions

1.Declare a two-dimensional array of Strings namedchessboard.
1. Declare a two-dimensional array of Strings named chessboard.2. Declare a two-dimensional array of integers named tictactoe.3. Declare and create a two-dimensional array of chars,tictactoe, with 3 rows, each with 3 elements.4. Create a two-dimensional array of ints, plan, with 2 rows, and and 3 columns and initialize the first row to 8, 20, 50 and the second row to 12, 30, 75. Use member initializer syntax.
3. Write a Java program that generates a set of random strings from a given string...
3. Write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and characters belong to the original string. Examples Input: “Hello World” Output: “World oHlel”
In C++ please modify the following program and add characters (char) and names (strings) to be...
In C++ please modify the following program and add characters (char) and names (strings) to be added to the linked list along with integers. The current demo program accepts only integer data, so you would ask the user to select the data type to be added to the linked list. The user should be given the following three choices: (a) whole numbers (b) single characters (c) strings Once the user makes a selection from the list above then your program...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a program that prompts the user to enter two characters and display the corresponding major...
Write a program that prompts the user to enter two characters and display the corresponding major and year status. The first character indicates the major. And the second character is a number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. We consider only the following majors: B (or b): Biology C (or c): Computer Science I (or i): Information Technology and Systems M (or m): Marketing H (or h): Healthcare Management...
Write a progrm that accepts two strings as input, then indicates if the two strings are...
Write a progrm that accepts two strings as input, then indicates if the two strings are equal when the case of individual letters is ignored. Sample runs are shown below. Use C++ Enter two strings. String 1: PROgraM String 2: proGram PROgraM and proGram are equal. Enter two strings. String 1: litter String 2: LittLe litter and LittLe are not equal.
Please write in x86 Assembly language on Visual Studio Write a program to compare two strings...
Please write in x86 Assembly language on Visual Studio Write a program to compare two strings in locations str1 and str2. Initialize str1 to Computer and initialize str2 to Compater. Assume Str1 holds the correct spelling and str2 may have an incorrect spelling. Use string instructions to check if str2 is correct and if not correct the mistake in str2.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT