Question

In: Computer Science

I need to write java program that do replace first, last, and remove nth character that...

I need to write java program that do replace first, last, and remove nth character that user wants by only length, concat, charAt, substring, and equals (or equalsIgnoreCase) methods.

No replace, replaceFirst, last, remove, and indexOf methods and letter case is sensitive.

if user's input was "be be be" and replace first 'b'

replace first should do "e be be"

replace last should do "be be e"

remove nth character such as if user want to remove 2nd b

it should be like "be e be"

Solutions

Expert Solution

Solution:

import java.util.Scanner;

public class Replace {
    public static void main(String[] args) {
        String input;
        String first = "", last = "", n = "", x;
        int choice, i, index, count = 0;

        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string:");
        input = in.nextLine();

        while (true) {
            System.out.print("\n1.Replace First\n2.Replace Last\n3.Replace nth\n4.Exit\n");
            System.out.print("Enter your choice:");
            choice = in.nextInt();
            switch (choice) {
                case 1:
                    System.out.print("Enter the character whose first occurrence has to be replaced:");
                    x = in.next();
                    for (i = 0; i < input.length(); i++) {
                        if (input.charAt(i) == x.charAt(0)) {
                            first = input.substring(0, i) + input.substring(i + 1);
                            break;
                        }
                    }
                    System.out.print("Result is " + first);
                    break;
                case 2:
                    System.out.print("Enter the character whose last occurrence has to be replaced:");
                    x = in.next();
                    for (i = input.length() - 1; i >= 0; i--) {
                        if (input.charAt(i) == x.charAt(0)) {
                            last = input.substring(0, i) + input.substring(i + 1);
                            break;
                        }
                    }
                    System.out.print("Result is " + last);
                    break;
                case 3:
                    System.out.print("Enter the character whose nth occurrence has to be replaced:");
                    x = in.next();
                    System.out.print("Enter the value of n:");
                    index = in.nextInt();
                    for (i = 0; i < input.length(); i++) {
                        if (input.charAt(i) == x.charAt(0))
                            count++;
                        if (count == index)
                            break;
                    }
                    n = input.substring(0, i) + input.substring(i + 1);
                    System.out.print("Result is " + n);
                    break;
                case 4:
                    System.out.print("Bye\n");
                    System.exit(0);
            }
        }
    }
}

Output:


Related Solutions

So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
In Java, write a program that finds the first character to occur 3 times in a...
In Java, write a program that finds the first character to occur 3 times in a given string? EX. Find the character that repeats 3 times in "COOOMMPUTERRRR"
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Write a program to remove extra blanks from text files. Your program should replace any string...
Write a program to remove extra blanks from text files. Your program should replace any string of two or more blanks with one single blank. It should work as follows: * Create a temporary file. * Copy from the input file to the temporary file, but do not copy extra blanks. * Copy the contents of the temporary file back into the original file. * Remove the temporary file. Your temporary file must have a different name than any existing...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
java programming write a program with arrays to ask the first name, last name, middle initial,...
java programming write a program with arrays to ask the first name, last name, middle initial, IDnumber and 3 test scores of 10 students. calculate the average of the 3 test scores. show the highest class average and the lowest class average. also show the average of the whole class. please use basic codes and arrays with loops the out put should look like this: sample output first name middle initial last name    ID    test score1 test score2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT