Home > Back-end >  How to clear a string after class is used?
How to clear a string after class is used?

Time:07-05

This is a palindrome algorithm that I'm working on. It works only with one exception. Instead of clearing out a string, it appends it. I tried a couple of examples. The ones that I tried are commented out. I still want to keep the palindrome class as a separate class. What would be the best way to clear reverse or sinput screen each time while loop runs?

import java.util.*;
import java.io.*;
    
public class palindrome {
    String reverse = "";

    palindrome() {
    }

    ;

    String reverseStr(String inputs) {
        for (int i = inputs.length() - 1; i >= 0; i--) {
            reverse  = inputs.charAt(i);
        }
        return reverse;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method 
        palindrome sinput = new palindrome();
        int choice = 1;
        Scanner scan = new Scanner(System.in);
        while (choice <= 1) {

            System.out.println("Print new word");
            String inputs = scan.nextLine();

            String reversed = sinput.reverseStr(inputs);
            System.out.println(reversed);
            //   reversed.clear();   Doesn't work creates Complier error
            //  reversed.nextLine();  Doesn't work creates Complier error
            scan.reset(); // Doesn't work 
            //reversed.fill(reversed,0); Doesn't work 
            System.out.println("To Continue with new word Enter 1 ; to quit Enter One");

            String choiceString = scan.nextLine();
            choice = Integer.parseInt(choiceString);

            if (choice == 1) {
                continue;

            } else if (choice > 1) {
                break;
            }
        }

        scan.close();
    }
}

CodePudding user response:

You can fix this by updating your reverseStr method by moving reverse into the method which will effectively have a fresh string every time you call reverseStr, otherwise you can use reverse = ""; before the method to "reset" it. Here is a working solution:

String reverseStr(String inputs) {
    //Move the reverse variable inside you mothed so that it gets reset every time reverseStr is called
    String reverse=""; 

    //Now do the loop
    for (int i =inputs.length()-1; i>=0; i-- ) {
        reverse =inputs.charAt(i);      
    }
    return reverse;
}

CodePudding user response:

Instead of iterating through a string character-by-character and reinventing the wheel:

"Why not just use what Java already has built-in?"

Also, you don't need a variable which complicates and gives you further problems.

    String reverseStr(String inputs) {
      return new StringBuilder(inputs).reverse().toString();
    }

Edit:

    // Add the import before the class:
    import java.lang.StringBuilder;

Last Edit:

This has already been tested on JShell, built in with the JDK

    jshell> import java.lang.StringBuilder;

    jshell> System.out.println(new StringBuilder("Testing input").reverse().toString());

    tupni gnitseT 
  •  Tags:  
  • java
  • Related