سؤال

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!

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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");

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top