Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error - start of expression

Status
Not open for further replies.

tberger1

MIS
Sep 12, 2006
16
US
I am new to Java so I am trying to write some simple applications that I will need to enhance later for a new project I need to implement.

The program I am writing should be simple. It removes one word from a statement and changes it with another. I thought a simple getChars would do it but when I try to compile it, I get a start of expression error and I am just not getting things enough yet to find the error.

The code is:
/**
Replaces the first occurrence of "hate" in a String with the word "love".
*/
public class LoveHate
{
/**
The String in which a word is to be replaced
*/

public static final String ORIGINAL = "I hate you.";

public static void main(String[] args)
{
// Print the original string
System.out.println("The line of text to be changed is:");
System.out.println(ORIGINAL);



public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
{

String REPLACE = "Love";

srcBegin = REPLACE(0);
srcEnd = REPLACE(3);
dst = ORIGINAL(2);
dstbegin + (srcEnd-srcBegin) - 1


}


// Construct and print the changed text
System.out.println("I have rephrased that line to read:");
System.out.println(start + "love" + end);
}

}

Am I even close here?

Any help with this is really appreciated.

Thanks,
TB
 
I cannot rebuild your code. but if you want to replace a word within a string, you can use replace(String a,String b).

Code:
String a="I hate you.";
a.replace("hate", "love");

Chinese Java Faq Forum
 
Code:
           srcBegin = REPLACE(0);
            srcEnd = REPLACE(3);
            dst = ORIGINAL(2);

Looks like you're trying to code C-style. Wellcome to Java, where there are no pointers anymore. I'd suggest you to take a tutorial about Java basics.

Cheers,
Dian
 
I changed it to use the replaceFirst:

/**
Replaces the first occurrence of "hate" in a String with the word "love".
*/
public class LoveHate
{
/**
The String in which a word is to be replaced
*/

public static final String ORIGINAL = "I hate you.";

public static void main(String[] args)
{
// Print the original string
System.out.println("The line of text to be changed is:");
System.out.println(ORIGINAL);

ORIGINAL.replaceFirst("hate", "love");


// Construct and print the changed text
System.out.println("I have rephrased that line to read:");

System.out.println(ORIGINAL);

}
}

But it still prints I hate you both times. Any other thoughts to get me through this?
 
Hi

[tt]final[/tt] means can not change its value, so do not made it [tt]final[/tt]. And [tt]String[/tt] is immutable, so the methods does not change its value, but returns the changed value.
Code:
/**
  Replaces the first occurrence of "hate" in a String with the word "love".
 */
public class LoveHate
{
  /**
  The String in which a word is to be replaced
  */

  public static String original = "I hate you.";

  public static void main(String[] args)
  {
    // Print the original string
    System.out.println("The line of text to be changed is:");
    System.out.println(original);

    original=original.replaceFirst("hate", "love");

    // Construct and print the changed text
    System.out.println("I have rephrased that line to read:");
    System.out.println(original);
  }
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top