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!

pei wang
  • 295
  • 2
  • 8
  • 17

4 Answers4

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...

ppeterka
  • 20,583
  • 6
  • 63
  • 78
  • Thanks ppeterka, great help, so in which scenario I need to create a loop to read word by word? – pei wang Oct 11 '13 at 15:43
  • 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 ` `, 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...) – ppeterka Oct 11 '13 at 15:50
  • 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? – pei wang Oct 11 '13 at 15:56
  • @peiwang In that case, yes, int that case, you will have to use a loop to process each word. – ppeterka Oct 11 '13 at 19:06
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");
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
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
An SO User
  • 24,612
  • 35
  • 133
  • 221
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.

CODEBLACK
  • 1,239
  • 1
  • 15
  • 26