d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Problem With String Replacement
Add Reply New Topic New Poll
Member
Posts: 3,710
Joined: May 5 2008
Gold: 165.52
Sep 25 2012 12:11pm
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
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 25 2012 02:55pm
Replace takes two arguments: what to find and what to replace with. you're only providing one argument.

When you have a compile time error, just post the offending line (along with variable declarations). no need to post everything.

Tip in the future: stop nesting so many functions if you have problems.

Instead of: System.out.println(text.replace(sc.nextLine()));

do:

log.trace("text=" + text);

String nextLine = sc.nextLine();
log.trace("nextLine=" + nextLine);

String replacement = text.replace(..);
log.trace("replacement=" + replacement);

System.out.println(replacement);

when you run into problems, you can clearly see the values of all your variables without having to use a debugger.
Member
Posts: 3,710
Joined: May 5 2008
Gold: 165.52
Sep 27 2012 12:09pm
What exactly does ''log.trace'' do?

edit, tried your code but it comes up with the error: 'malformed floating point literal' - what does this mean?

This post was edited by Absolution on Sep 27 2012 12:12pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll