Forums | Hello Guest, Login or Register | > > L4Y Files: No longer here! < <
  Levels-4-You : Lounge : Programming help [Forum Rules]  


Back to Forum Thread List

Tech_N9ne   Posted 3rd Mar 2006 1:54am
L4Y Member
Post 96 / 163

Ok, I've been working/ modifying this code for some 6 hours, and still can't get it to work properly! The program is in java and the code is the begininng of a larger spell check program...

This particular code needs only to read a file, find the string "red" and write to a file with "red" being changed to "blue" Any help is appreciated...

(Please, no spam/ useless posts in this thread) Ok, heres the code...

Code

public class SpellCheck
{
    
    
     public static void readFile(String filename)
     {
          try
          {
               BufferedReader br = new BufferedReader(new FileReader("blah.txt"));
               BufferedWriter bw = new BufferedWriter(new FileWriter("blahout.txt"));
               String line = br.readLine();
               line = line.toLowerCase();
               String[] tokens = line.split(" ");
              
               while (line != null)
               {
                    for(int i = 0; i < tokens.length; i++)
                    {
                         if(tokens[i] == "red")
                         {
                              bw.write("blue");    
                         }
                   
                         else
                         {
                              bw.write(tokens[i] + " ");    
                         }    
                        
                    }
                    bw.close();
               }
         
          }
         
          catch(java.io.IOException e)
          {
               System.out.println(e);
          }
         
     }

     public static void main(String[] args)
     {
         
          readFile("blah.txt");
     }

}


In yellow is the loop/ condition I do not understand why it will not execute, once again, help is greatly appreciated...
... War of the masses, the outcome, disastrous,
Many of the victim family save they ashes ...
 
 
Saikou   Posted 3rd Mar 2006 3:53am
L4Y Member
Post 59 / 93

I suspect this to be the problem, unless the string 'red' is in the first line of the input file, in which case I have no idea

Anyway, inside your "while (line != null)" condition, you never modify line, so once execution gets in there, it can't get out unless an exception occurs. And since line never gets modified (i.e. it doesn't read the next line once you finish checking the first one) then it'll never manage to read past the first line in the file.

Personally, I'd change the while condition to both read and test in one statement, and move the parts that convert it to lowercase and tokenise it inside the while.

I.E, this should work (no guarantees though, it's untested):
Code

...

BufferedReader br = new BufferedReader(new FileReader("blah.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("blahout.txt"));

String line;
while (line = br.readLine())
{
     line = line.toLowerCase();
     String[] tokens = line.split(" ");

     for(int i = 0; i < tokens.length; i++)
     {
          if(tokens[i] == "red")
          {
               bw.write("blue");
          }
          else
          {
               bw.write(tokens[i] + " ");
          }

     }
}
bw.close();

...
watashi no chikara de susumu hate shinai kono michi o
C++ World  
 Modified Mar 3rd, 03:55am by Saikou
Tech_N9ne   Posted 3rd Mar 2006 4:02am
L4Y Member
Post 97 / 163

Outstanding suggestion, but the txt file is only one line, containing maybe 10 words(for debugging purposes, start small )

I don't have my IDE up(on a remote machine), but I will give it a try.

Your input, Saikou is greatly appreciated!!! Thanks man!

Edit: Im such an idiot, I forgot java is retarded and you need .equals as opposed to = Problem solved
... War of the masses, the outcome, disastrous,
Many of the victim family save they ashes ...
 
 Modified Mar 3rd, 07:47pm by Tech_N9ne



Copyright © 2000-2020 Levels-4-You
Your request was handled in 0.09 seconds.