0

I got a query, please see code below:

        public void readFile(String path,String pathName,int num){
            try{
        PrintWriter out2=new PrintWriter(new PrintWriter(path));
        File a=new File(pathName);
        Scanner b=new Scanner(a);


        while(b.hasNextLine()){
        String message=b.nextLine();
        Scanner h=new Scanner(message);

        while(h.hasNext()){
            String f=h.next();
            if (f.equals("are")){
                f.replace("are","ARE");

            }

            }

    out2.printf("%s",message);
    out2.println();
    .......

The file content for scanner read is

    who are you?
    how are you? 
    what is up!

However, when I run the above codes and the output to the new file are the same with the input file, it means the "are" not replaced by "ARE", I have no idea which part is wrong, please advise, thanks guys!

2
  • 2
    You're calling String.replace and ignoring the output. Strings are immutable in Java. Commented Oct 11, 2013 at 15:33
  • @JonSkeet is right. You should have a look at the docs :) Commented Oct 11, 2013 at 15:33

4 Answers 4

3

This line just outputs the message unchanged to the new file.

out2.printf("%s",message);

Also the loop is strange too: why do you read it word by word, and then use String.replace()? You could do it line by line, using String.replaceAll():

   while(h.hasNextLine()){
       String message=b.nextLine();
       out2.printf("%s",message.replaceAll("(^|\\W)are(\\W|$)"," ARE "));
   }

The (^|\\W)are(\\W|$) string is a regular expression, having the meaning to match all content, that starts with either being the start of the string ^, or a non-word character (\\W), the string are, and ends with a non-word character or the end of line($)...

As scanner has whitespace as the default delimiter, it might be ever better to use (^|\\s)are(\\s|$), however both these will replace the whitespace before and after "ARE" with a single space ()...

Also, keep in mind, that String.replace does not mutate the input String... You have to assign the result, or use it any other way, like pass it to a function...

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks ppeterka, great help, so in which scenario I need to create a loop to read word by word?
When you want to do something more complex than just swap it for another word. Or for example zou have an input that is always like <word> <word> <number> <word>, and you want every 3rd token to be read by Scanner.nextInt() to get an integer... (but as we speak, it would be possible to get a regex for this too...)
Hi ppeterka, so if I want to use charAt or indexOf for individual word or letter handling, i need to create loop to read word by word, right?
@peiwang In that case, yes, int that case, you will have to use a loop to process each word.
1

String is final and immutable, which is the same.

so f.replace("are","ARE"); must be inserted into a new or not variable.

f = f.replace("are","ARE");

Comments

0

I do not understand why you are doing that. Here is an alternative approach:

  1. Get a BufferedReader to read the file.
  2. While there is data in the file, read the lines.
  3. If line.contains("are") then line = line.replace("are","ARE")
  4. println(line)

As to why your code did not work:
In this line, f.replace("are","ARE"); You forgot to get the output.
Make it as such: message = f.replace("are","ARE");


Another option is to use StringBuffer or StringBuilder

Comments

0

Strings are immutable. Therefore, you can not run the replace method on object f and expect its value to be changed since the replace method of a string object will simply return a new String object.

either use a StringBuilder instead, or use :

f = f.replace

On the other hand, StringBuilder objects are mutable. Therefore, you can run the StringBuilder version of the replace method directly on the object if you choose that route instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.