Im guessing an error that says that my method is not suitable for string replacement. Ive also tried .replaceAll , with no luck.
This is my code:
import java.util.Scanner;
import java.util.regex.*;
public class pre_release
{
public static void main(String[] args)
{
String text, searchString;
int menuChoice = 0;
Scanner sc = new Scanner(System.in);
//input text
System.out.print("Text: ");
text = sc.nextLine();
//display menu and get choice
System.out.print("Menu:\n1\tlength\n2\tequals\n3\tcontains\n4\textract\n5\treplace\nchoice: ");
if (sc.hasNextInt())
{
menuChoice = sc.nextInt();
sc.nextLine(); //purge the keyboard buffer - sometimes neeeded
}
else System.exit(0);
//menu item 1 - print number of characters in text
if (menuChoice == 1)
{
int text_length = text.length();
System.out.println("The length of the text entered is: " + text_length);
sc.nextLine();
}
//menu item 2 - verify text
if (menuChoice == 2)
{
String secondText;
System.out.print("text again: ");
secondText = sc.nextLine();
if (text.equals(secondText))System.out.print("Exact match ");
else System.out.print("NOT same text!");
}
//menu item 3 - check whether text contains a search string
if(menuChoice == 3)
{
System.out.print("Search the text for: ");
String search = sc.nextLine();
if(text.contains(search))
{ System.out.println("Found");
}
else System.out.println("Not found");
}
//menu item 4 - extract a section from text
else if (menuChoice == 4)
{
int startPos, endPos;
System.out.print("Extract start position (inclusive - lowest postion is 0): ");
startPos = sc.nextInt();
sc.nextLine();
System.out.print("Extract end position (inclusive - not less than start position - highest position is " + (text.length()-1) + "): ");
endPos = sc.nextInt();
sc.nextLine();
if (startPos >= 0 && endPos < text.length() && startPos <= endPos)
System.out.println("Extracted text: " + text.substring(startPos,endPos + 1));
else System.out.println("Invalid text position(s)");
}
//menu item 5 - replace something within text
if(menuChoice == 5)
{
System.out.println("Enter the new characters you want to replace, followed by the characters being replaced, separated by a comma.");
System.out.println(text.replace(sc.nextLine()));
}
//non menu items
else System.out.println("Not a valid choice.");
}//end main
}//end class
The lines bolded is where the code has gone wrong, someone please help