السلام عليكم ورحمه الله وبركاتة
انا عملت برنامج بيقرا من ملف txt ويبحث عن اى كلمة عايزها
حاجة كدة زى ال find فى ال office
انا ليا تجربتيين فى البنامج :
التجربة الاولى :
/*****************************************************************
This is a program for finding the occurence of a word in a file *
*****************************************************************/
import java.io.File;
import java.util.Scanner;
public class WordOccurence {
public static void main(String[] args) throws Exception {
if(args.length != 2) {
System.out.println("WordOccurence class -> How to use :\njava WordOccurence fileName wordToFind");
}
File file = new File(args[0]);
if(!file.exists()) {
System.out.println("The file " + args[0] + " doesn't exist");
System.exit(0);
}
int count = 0;
Scanner input = new Scanner(file);
while(input.hasNext()) {
String s1 = input.next();
if(s1.matches(".*?" + args[1] + ".*?") || s1.equalsIgnoreCase(args[1]) || s1.matches(".*?" + args[1].toUpperCase() + ".*?") || s1.matches(".*?" + args[1].toLowerCase() + ".*?")) {
System.out.print((count + 1) + " - " + s1 + " ");
count++;
}
}
System.out.println("\n\nThe word \"" + args[1] + "\" have been repeated " + count + " times in the file " + args[0]);
input.close();
}
}
والتجربة الثانية :
* A program for finding and counting the occurence o a word in a file *
***********************************************************************/
/***************************************************
* importing the class File *
* that is used to create a File object for a file *
***************************************************/
import java.io.File;
/*********************************************
* importing the class Scanner *
* that is used to read an input from a file *
*********************************************/
import java.util.Scanner;
public class WordOccurence {
/*********************
** The main method **
*********************/
public static void main(String[] args) throws Exception {
//The word we wand to find in the file
String s2 = "naruto";
File file = new File("naruto.txt");
Scanner input = new Scanner(file);
//A counter to found the number of occurences of the word in the file
int count = 0;
//To return the line number where we found the word
int lineCount = 1;
//A loop to find the word in the file
while (input.hasNextLine()) {
do {
/* if there is a next line in the file
* we will see if there is a next String in the line
* we will use space as a delimiter
* and take every word in the line and compare it with word we want to find
*/
input.useDelimiter(" ");
String s = input.next();
if (s.matches(".*?" + s2 + ".*?") || s.equalsIgnoreCase(s2) || s.matches(".*?" + s2.toUpperCase() + ".*?") || s.matches(".*?" + s2.toLowerCase() + ".*?")) {
System.out.print((count + 1) + " - " + s + " " + lineCount + " ");
count++;
}
} while (input.hasNext());
lineCount++;
}
//Printing the occurences of the word
System.out.println("\n\nThe word \"" + s2 + "\" have been repeated " + count + " times in the file naruto.txt");
input.close();
}
}
/***********************************************************************
انا لسة ماتعلمتش GUI بس لما اتعلمها ان شاء الله هاعمل البرنامج دة بيها
بس للاسف الحلو م يكملش
انا بقال اسبوعيين مش عارف اخلصة لان انا محتاج كام حاجة كدة فى البنامج دة :
انا وصلت انى اقدر اجد الكلمة فى السطر بس واطبع رقم السطر وعدد تكرار الكلمة فى السطر
1 - انا محتاج اجد الكلمة فى البنامج كله مش السطر بس
انا وصلت انى اقدر ابحث عن الكلمة واطلع اى كلمة زيها سواء lower case او upper case او معاها كلمة تانية الخ
2 - انا لسه مش عارف اطلع كل الكلمات من الملف الشبيها بالكلمة اللى انا ببحث عنها لسة فى كام حالة مش عارف اطلعها
يا رييت تجربو البنامج على الملف اللى فى المرفقات
لو اللى فى المرفقات مش تمام ادخلو
هناالتجربة الاولى انا مستخدم ال args بتاعة ال main لكى تقوم بادخال اسم الملف ثم الكلمة المراد البحث عنها فى ال consol
انا بطلع كل الكلمات بس مش عارف اطلع رقم السطر على اى ملف
والتجربة التانية بطلع كل الكلمات اللى فى السطر بس انا عايز الملف كله
يا رييت تكونو فهمتونى وتقدروا تساعدونى
سلام
