أود الحصول على كود لبرنامج يقوم بتغير الكلمة التي يدخلها المستخدم مع كلمة أخر يحددها المستخدم أيضاً
بالتأكيد لملف من نوع وورد أو تكست يقوم المستخدم بتحديد الملف.
الرجاء المساعدة بأسرع وقت ممكن. ولكم جزيل الشكر:o
أود الحصول على كود لبرنامج يقوم بتغير الكلمة التي يدخلها المستخدم مع كلمة أخر يحددها المستخدم أيضاً
بالتأكيد لملف من نوع وورد أو تكست يقوم المستخدم بتحديد الملف.
الرجاء المساعدة بأسرع وقت ممكن. ولكم جزيل الشكر:o
u must correct tow proplems
1. if the Key is in the start
2. the newline in the Out text isn't exsist
try to correct it, when u have aproplem plese write me
/*this Program for replace String in Textfile */
import java.io.*;
public class FileReplace
{
FileReader _idocfile=null;
BufferedReader _buffin=null;
FileWriter _outdocfile = null;
BufferedWriter _buffout = null;
String _inFileName=null;
String _outFileName=null;
int count=0;
public FileReplace(String str)
{
_inFileName=str;
_outFileName="Replace"+str;
}
public int getCount()
{
return count;
}
public void writeFile(String str)
{
try
{
_outdocfile = new FileWriter(_outFileName);
_buffout = new BufferedWriter(_outdocfile);
_buffout.write(str);
}
catch (FileNotFoundException e)
{
System.out.println(e.toString());
}
catch (IOException e)
{
System.out.println(e.toString());
}
try
{ _buffout.close();
_outdocfile.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public String replaceString(String str, String key,String rep)
{
StringBuffer strbuf=new StringBuffer();
int pos=0;
int index=-1;
int length1=0;
int lehngth2=0;
String rest1=str;
String temp=null;
count=0;
try
{
while((rest1!=null)&&(rest1.length()>0)&&(index=rest1.indexOf(key))!=0)
{
if(index >0)
{
count++;
temp=rest1.substring(0,index)+rep;
strbuf.append(temp);
rest1=rest1.substring(index+key.length());
}
else
{
strbuf.append(rest1);
rest1="";
}
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
return strbuf.toString();
}
public String readFile()
{
String quell ="";
String in;
try
{
_idocfile = new FileReader(_inFileName);
_buffin = new BufferedReader(_idocfile);
StringBuffer strBuf=new StringBuffer();
//quell = strBuf.toString();
while ((in = _buffin.readLine())!= null) // Datei satzweise lesen
{
//löscht Hochkoma
strBuf.append(in);
}
quell=strBuf.toString();
}
catch (FileNotFoundException e)
{
System.out.println(e.toString());
}
catch (Exception e)
{
System.out.println(e.toString());
}
finally
{
try
{
_buffin.close();
_idocfile.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
return quell;
}
public static void main(String[] args)
{
FileReplace myRepl=new FileReplace("test.txt");
String str=myRepl.readFile();
System.out.println("Text Befor replace is:");
System.out.println(str);
String str2=myRepl.replaceString(str, "the","xxx");
System.out.println("Text After replace is:");
System.out.println("nthey are "+ myRepl.getCount()+" words repleased");
System.out.println(str2);
myRepl.writeFile(str2);
myRepl=null;
}
}Thank you for giving help:o
I will try to fix the second problem
but I did not not understand part 1:(
. if the Key is in the start
what do you want to say
"sorry
I mean in the begin of the Text
try with a file "test.txt" and write a word "the" in the begin of this text.
if you have a proplem, you can write me or send me E-mail and i'll help you!!!
Did you understand the programm ? and did you excute it ?
you can answer in arabic. I can read but I haven't a system of arabic !
;)
//Here is the Correct Answer for Problem 1
public String replaceString(String str, String key,String rep)
{
StringBuffer strbuf=new StringBuffer();
int pos=0;
int index=-1;
int length1=0;
int lehngth2=0;
String rest1=str;
String temp=null;
count=0;
try
{
/*Change for Problem 1*/
if(rest1.indexOf(key))==0)
{
count++;
temp=rep;
strbuf.append(temp);
rest1=rest1.substring(key.length());
}
/* End of the change*/
while((rest1!=null)&&(rest1.length()>0)&&(index=rest1.indexOf(key))!=0)
{
if(index >0)
{
count++;
temp=rest1.substring(0,index)+rep;
strbuf.append(temp);
rest1=rest1.substring(index+key.length());
}
else
{
strbuf.append(rest1);
rest1="";
}
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
return strbuf.toString();
}/* the Correct Answer for Problem 2 */
public String readFile()
{
String quell ="";
String in;
try
{
_idocfile = new FileReader(_inFileName);
_buffin = new BufferedReader(_idocfile);
StringBuffer strBuf=new StringBuffer();
while ((in = _buffin.readLine())!= null)
{
strBuf.append(in);
strBuf.append("n");//Change for Problem 2
}
quell=strBuf.toString();
}
catch (FileNotFoundException e)
{
System.out.println(e.toString());
}
catch (Exception e)
{
System.out.println(e.toString());
}
finally
{
try
{
_buffin.close();
_idocfile.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
return quell;
}أشكرك أخي khwlani
ولكن لفهم الكود أحتاج لبعض الوقت فأنا طالب مبتدئ.
كما أنني أعتقد بأن الكود معقد نوعاً ما.
تصورت لعمل مثل هذا البرنامج أن أقوم بتعريف 2Arrays وأن أستخدم StringTokenizer يمنحني القدرة على تحويل النص إلى Array1 وبعد ذلك أقوم بمقارنة الكلمة المدخلة من قبل المستخدم مع جميع عناصر Array1 وأقوم بتخزين جميع الكلمات في Array2 بعد تغير الكلمة المطلوبة. ثم أقوم بطباعة Array2 داخل ملف وورد.
هذا هو الكود الذي كتبه مع العلم أنه لم يعمل بشكل صحيح
import java.io.*;
import java.util.*;
public class FileR1
{
public static void main (String []args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
BufferedReader read= new BufferedReader(new FileReader("coe200.doc"));
PrintWriter write;
StringTokenizer Token;
System.out.println("input the word");
String word = stdin.readLine();
String line;
String name;
String [] Otext;
String [] Ctext;
Ctext = new String[1000000];
Otext = new String[1000000];
int i = 0;
while(( line=read.readLine())!=null)
{
Token = new StringTokenizer(" ");
while((Token.hasMoreTokens()))
{
name = Token.nextToken();
Otext = name;
if (name!=word)
Ctext = name;
else Ctext = word;
i++;
}
//System.out.println(" xxxxx " +Ctext);
write=new PrintWriter(new FileWriter("TR1.doc"));
write.println(Ctext);
write.close();
}
}
}الرجاء مساعدتي.هل المنطق الذي أفكر به خاطئ
ما الذي يلزمني لجعل هذا البنامج يعمل كما هو مطلوب.
لقد قمت بتشغيل الكود الذي وضعته ولكنه لم يطلب أن احدد الكلمة التي أريد تغيرها بل طبع ملف آخر وجعل النص في سطر واحد دون أن يجري أي تغير.
بالنسبة للحلول التي طرحتها سأقوم بتجربتها وأخبرك بالنتائج.
String[1000000] is very wrong
How match Byte can in RAM resived?
Compare with all Words is very slow
replace for the Doc-Files with end *.doc (MsWord) is complex;
import java.io.*;
import java.util.*;
public class FileReplaceNew
{
public static void main(String[] args)
{
try
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("n enter the Filename:");
String filename = stdin.readLine();
System.out.println("n enter the word befor the replaced");
String fromWord = stdin.readLine();
System.out.println("n enter the word after the replaced");
String toWord = stdin.readLine();
String outFileName="Replace"+filename;
System.out.println(filename+" ------- "+fromWord+" ------ "+toWord);
String sourceText="";
String resultText="";
String in=null;
FileReader idocfile = new FileReader(filename);
BufferedReader buffin = new BufferedReader(idocfile);
while ((in = buffin.readLine())!= null)
sourceText+=(in+"n") ;
buffin.close();
idocfile.close();
StringTokenizer Token = new StringTokenizer(sourceText,fromWord);
while((Token.hasMoreTokens()))
resultText +=(Token.nextToken()+toWord);
FileWriter outdocfile = new FileWriter(outFileName);
BufferedWriter buffout = new BufferedWriter(outdocfile);
buffout.write(resultText);
buffout.close();
outdocfile.close();
System.out.println("Text Befor replace is:");
System.out.println(sourceText);
System.out.println("Text After replace is:");
System.out.println(resultText);
}catch(Exception e)
{
System.out.println(e.toString());
}
}}
هذا الموضوع مغلق.