الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

[تنبيه - عنوان غير مناسب : ]عايز مساعدة فى البنامج دة ؟؟

بدأه NarutoMan في 14 نوفمبر 2010 · 9 رد · 641 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمه الله وبركاتة

انا عملت برنامج بيقرا من ملف 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

انا بطلع كل الكلمات بس مش عارف اطلع رقم السطر على اى ملف

والتجربة التانية بطلع كل الكلمات اللى فى السطر بس انا عايز الملف كله

يا رييت تكونو فهمتونى وتقدروا تساعدونى

سلام

naruto.txt

تم تعديل هذه المشاركة بواسطة NarutoMan في 14 نوفمبر 2010 في 14:23

#2

طب ما تستخدم الـ BufferedReader احسن من الـ Scanner

وتقدر تعمل اللى انت عاوزه بيها

انما الطريقه دى حاسس انه معقده :(

متعاملتش مع الـ Scanner بالطريقه دى خالص

Software Developer
Mahmoudkelany.com


 

#3

يا راجل انت شايف كدة

اممم

بس انا معرفش حاجة عن ال BufferedReader

وبعدين الاحسن Buffer ولا Scanner

تم تعديل هذه المشاركة بواسطة NarutoMan في 14 نوفمبر 2010 في 15:00

#4
NarutoMan كتب:

يا راجل انت شايف كدة

اممم

بس انا معرفش حاجة عن ال BufferedReader

تتعلم يا حماده :)

شوف هذا هيساعدك ان شاء الله

http://download.orac...eredReader.html

http://www.java2s.co...feredReader.htm

ودى روابط من منتدانا الحبيب :)

http://www.arabteam2...howtopic=125547

ودا بقا اللى اعتقد انه هيفيدك اكتر

/index.php?showtopic=167926&st=40&p=1049807entry1049807

تم تعديل هذه المشاركة بواسطة Mahmoud Kelany في 14 نوفمبر 2010 في 15:00

Software Developer
Mahmoudkelany.com


 

#5

هاشوف كدة يمكن افهم

اصل البعيد(انا) غبى

#6
NarutoMan كتب:

هاشوف كدة يمكن افهم

اصل البعيد(انا) غبى

لا طبعا هتفهم

ولو فيه حاجه اسال

انا عملت الكود بتاعك بيمر على كل الصفوف بس انى اطلع رقم الصف مش ظابطه معايا

هاشوف تانى واقولك

بس ذاكر انت فاللى انا قولته ليك

import java.io.File;
import java.util.Scanner;

/**
 * 
 * @author Mahmoud Kelany
 */
public class WordOccurence {

	public static void main(String[] args) throws Exception {
    	//The word we wand to find in the file
    	String s2 = "naruto";
    	File file = new File("d:/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
    	do {
        	String s = input.next();
        	input.useDelimiter(" ");
        	if (s.matches(".*?" + s2 + ".*?") || s.equalsIgnoreCase(s2) || s.matches(".*?" + s2.toUpperCase() + ".*?") || s.matches(".*?" + s2.toLowerCase() + ".*?")) {
            	System.out.println((count + 1) + " - " + s2 + " " + lineCount + "  ");
            	count++;
        	}
    	} while (input.hasNext());
    	//Printing the occurences of the word
    	System.out.println("\nThe word \"" + s2 + "\" have been repeated " + count + " times in the file naruto.txt");
    	input.close();
	}
}

Software Developer
Mahmoudkelany.com


 

#7

الأخ الكريم/الأخت الكريمة

السلام عليكم ورحمة الله وبركاته

مرحباً بكم في منتدى الفريق العربي للبرمجة

نود تنبيهك أن العنوان غير مناسب.

الرجاء اختيار عنوان مناسب أكثر في مواضيعك القادمة، وهذا بمثابة إشعار للأهمية.

وفي حال التكرار سيتعرض موضوعك للإغلاق والحذف!!!

قواعد المشاركة

/index.php?showtopic=29343

شاكرين لكم حُسن تعاونكم

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#8

السلام عليكم

انا كنت متأكد ان الجملة دى هتتحط قبل العنوان

معلش اصل انا مش بعرف اختار اسماء اللموضوع بتاعى

لو عايزين تغيروة غيروة

وبعدين يا ريت تكون فهمت السؤال وتفيدنى فى الموضوع دة

ولا انت قبل العيد بتشتغل اشراف بس tongue.gif

كل سنة وانتم طيبيين

تم تعديل هذه المشاركة بواسطة NarutoMan في 14 نوفمبر 2010 في 23:58

#9

التجربة الأولي هي word count ولكنك لا تستطيع معرفة السطر بها، يمكنك استخدام LineNumberdReader بدلاً من Scanner في هذه الحالة اذا أردت ،،

التجربة الثانية أيضاً لا تخرج الأسطر لأن الحلقة الداخلية تقوم بأخذ كل الكلمات من الملف حتى النهاية..

يمكن التعديل بهذا الشكل:

public static void main(String[] args) throws Exception { 

	//The word we wand to find in the file 
	String s2 = "java"; 
	File file = new File("Notes.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 = 0; 

	//A loop to find the word in the file 
	while ( input.hasNext()  ) {
		lineCount++;

		String lineOfText = input.nextLine();
		String[] texts = lineOfText.split(" ");

		for (String s: texts) {
			if ( s.toLowerCase().matches(".*?" + s2.toLowerCase() + ".*?") ) { 
				System.out.println("Find:  " + s + " in line " + lineCount );
				count++;
			}
		}
	}

	//Printing the occurences of the word 
	System.out.println("\n\nThe word \"" + s2 + "\" have been repeated " + count + " times in the file " + file.getName()); 
	input.close(); 
}

بالتوفيق،

1

http://informatic-ar.com منصة تعليمية عربية في علوم الحاسب والبرمجة

https://moalfat.com  للكتب الالكترونية والكورسات التعليمية

Everything we see now is just an engineering solution based on old science

#10

+1

بسم الله ما شاء الله يا اخ Wajdy

تسلم ايدك انا كنت بردو عايز اعملها بطريقة ال split بس قولت انها هتاخد وقت طويل علشان تدور على كلمة فى ال array بس يبدو انه مفيش غيرها

انت كمان بدلت الجمل الكتيير اللى انا كاتبها فى ال if بجملة واحدة وطلعت كلماات اكتر

وطلعت رقم السطر للكلمة smile.gif

ربنا يجازيك خير

و شكرا على البرنامج

وشكرا ليك يا اخ علاء على تحويل الموضوع لسؤال

وعلى التحذير

ان شاء الله اخد بالى فى المواضيع اللى جاية من اسم الموضوع

سلام

تم تعديل هذه المشاركة بواسطة NarutoMan في 15 نوفمبر 2010 في 17:22

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…