Home > Back-end >  How can I take user input and replace the last 2 digits of the string with a certain character if it
How can I take user input and replace the last 2 digits of the string with a certain character if it

Time:10-05

I'm having a bit of a struggle with this. So far, I've been able to take user input in but I'm not sure what I'm doing wrong when it comes to replacing the last 2 digits IF they are 'x'.

class Main 
{
    public static String problem1_removeChars(String str) {
        Scanner myScanObj = new Scanner(System.in); //Create Scanner Object
        System.out.println("Enter your string: ");
        
        String readInput = myScanObj.nextLine(); //Read The Input
        System.out.println("Your String Is: "   readInput);
        int stringLength = readInput.length(); // Takes length of the users input
        
        if(stringLength >= 2) {
            if (str.charAt(stringLength - 2) == 'x') {
                str = str.substring(0, stringLength - 2)   str.charAt(stringLength - 1);
            }
        }
        if (str.length() >= 1) {
            if (str.charAt(str.length() - 1) == 'x') {
                str = str.substring(0, str.length() - 1);
            }
        }
        // IF the last 2 characters are 'x' THEN replace 'x' with '~nothing~' to remove x
        // return the string!
        return "str"; // FIX ME
    }
    public static void main(String[] args) {
        System.out.println(problem1_removeChars("str"));
    }
}

CodePudding user response:

The main problem is you're basically ignoring the user input (apart from reading the length of the String)

So, unless str and readInput are the same length, you'll run into issues.

Instead, start by removing the input parameter to the method, as it's confusing you. Replace uses of str with readInput and return readInput at the end

import java.util.Scanner;

public class Main {

    public static String problem1_removeChars() {
        Scanner myScanObj = new Scanner(System.in); //Create Scanner Object
        System.out.println("Enter your string: ");

        String readInput = myScanObj.nextLine(); //Read The Input
        System.out.println("Your String Is: "   readInput);
        int stringLength = readInput.length(); // Takes length of the users input

        if (stringLength >= 2) {
            if (readInput.charAt(stringLength - 2) == 'x') {
                readInput = readInput.substring(0, stringLength - 2)   readInput.charAt(stringLength - 1);
            }
        }
        if (readInput.length() >= 1) {
            if (readInput.charAt(readInput.length() - 1) == 'x') {
                readInput = readInput.substring(0, readInput.length() - 1);
            }
        }
        // IF the last 2 characters are 'x' THEN replace 'x' with '~nothing~' to remove x
        // return the string!
        return readInput; // FIX ME
    }

    public static void main(String[] args) {
        System.out.println(problem1_removeChars());
    }
}

CodePudding user response:

if (readInput.endsWith("xx")) {
    readInput = readInput.substring(0, readInput.length() - 2);
}

would surely be enough?

CodePudding user response:

The simple solution is to do something like this:

if (readInput.endsWith("xx"))
    return readInput.substring(0, readInput.length() - 2);
else
    return readInput;

You can also use regex:

return readInput.replaceAll("xx$", "");
  • Related