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

سؤال في ال Searching Array

مغلق
بدأه I love Java في 14 ديسمبر 2006 · 18 رد · 1,278 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

سؤالي هو:

كيف لو كنت أريد أن أطبع آخر ظهور للindex للكلمة التي أبحث عنها و ليس أول ظهور لها

مثلا :

في هذا الكلاس توجد كلمة "Ahamd" مرتين و أريد أن أطبع آخر ظهور لها و ليس أول ظهور لها و لكني لم أعرف

import javax.swing.*;
class SerachingArray{
public static void main(String[]args){

String[]entry={"Ahmad", "Mohammad", "Salem", "Ahmad","Salem"};
String name=JOptionPane.showInputDialog("Enter the name that you want to search for:");

	boolean found=false;
	int i=0;
	while((!found)&&(i<entry.length)){
		if (name.equals(entry))
		found=true;

		else
		i++;

	}//while


	if (found)
	JOptionPane.showMessageDialog(null,name+" is found"+ "\nindex is "+i);
	else
	JOptionPane.showMessageDialog(null,name+" is not found");

System.exit(0);
}
}
#2
import javax.swing.*;
class SerachingArray{
public static void main(String[] args){
int lastIndex=0;
String[]entry={"Ahmad", "Mohammad", "Salem", "Ahmad","Salem"};
String name=JOptionPane.showInputDialog("Enter the name that you want to search for:");

	boolean found=false;
	int i=0;
	while(i<entry.length){
		if (name.equals(entry)){
		found=true;
		lastIndex = i;
		}
		i++;

	}//while


	if (found)
	JOptionPane.showMessageDialog(null,name+" is found"+ "\n index is "+ lastIndex);
	else
	JOptionPane.showMessageDialog(null,name+" is not found");

System.exit(0);
}
}

الاختلافات :

1- قيمة i يجب أن تزداد سواء تحقق الشرط أم لم يتحقق فعليه أن يمر على كل عناصر المصفوفة

2- في حال تحقق الشرط يخزن قيمة i في متحول مساعد ويكمل البحث في بقية المصفوفة بهذا يتخزن آخر دليل.

3- الشرط لتوقف الحلقة يجب أن يكون الوصول لآخر المصفوفة فقط وليس العثور على النتيجة

فحنوت ألثم جرحه الرعافَ فانتكأت جراحي..

وهمت على خدي الدموع فقلت يا روحي وراحي..

هلّا رحمتَ قلوبنا .. وعدلت عن هذا الرّواحِ ِ ..؟

فأجابني البطل المسجّى هازئاً بيَ .. باقتراحي

كفكف دموعك ليس في عبراتك الحرّى ارتياحي

هذي سبيلي .. إن صدقت محبتي .. فاحمل سلاحي

Sun Certified Java Programmer

#3

طيب جربت التغييرات ولكنه يطبع لي أول ظهور وليس آخر ظهور..

يعني لم يتغير شي

وهل ممكن اني اطبع الاندكس لهم كلهم؟

وكيف اذا كان integer و ليس String

جربت في هذا الكلاس integer و يظهر لي error

import javax.swing.*;
class SearchLast{
public static void main(String[]args){

int[]entry={1,8,6,95,4,1,5,9,8,95};
String userInput=JOptionPane.showInputDialog("Enter the number that you want to search for:");
int number=Integer.parseInt(userInput);

	boolean found=false;
	int i=0;
	while((!found)&&(i<entry.length)){
		if (number.equals(entry))
		found=true;

		else
		i++;

	}//while


	if (found)
	JOptionPane.showMessageDialog(null,number+" is found"+ "\nindex is "+i);
	else
	JOptionPane.showMessageDialog(null,number+" is not found");

System.exit(0);


}
}

تم تعديل هذه المشاركة بواسطة I love Java في 15 ديسمبر 2006 في 19:45

#4

ارجوكم ساعدوني في الإجابه على اسئلتي

#5
import javax.swing.*;
class SearchLast{
	public static void main(String[]args){
		int[]entry={1,8,6,95,4,1,5,9,8,95};
		String userInput=JOptionPane.showInputDialog("Enter the number that you want to search for:");
		int number=Integer.parseInt(userInput);		
		boolean found=false;
		int i=0;
		String indexs="";
		while(i<entry.length){
			if (number==entry){
				found=true;
				indexs=indexs+"	"+i;	
			}

		//	else
				i++;		
		}//while		
		if (found)
			JOptionPane.showMessageDialog(null,number+" is found"+ "\nindex is "+indexs);
		else
			JOptionPane.showMessageDialog(null,number+" is not found");		
		System.exit(0);
	}
}
#6

مشكور اخوي والله يجزاك الف خير

#7

طيب لو سمحتوا سؤال أخير:

اذا طلع هذا الerorr ماذا يعني:

Expection in thread "main" java.lang.NoSuchMethodErorr: main
#8
 public static void main(String[]args){

هذا الاسلوب مش موجود في الكود حتى يتم تنفيذ ما هو بداخله

لست متواجد بشكل يومي

#9

الاسلوب موجود وهذا هو الكلاس:

import javax.swing.*;
class SearchLast{

public static boolean searchLast(int[] entry, int number){
	boolean found=false;
	int i=0;
	String indexs="";
	while(i<entry.length){
		if (number==entry){
		found=true;
		indexs=indexs+"	"+i;
	}
		i++;

	}//while

if (found)
	JOptionPane.showMessageDialog(null,number+" is found"+ "\nindex is "+indexs);
	else
	JOptionPane.showMessageDialog(null,number+" is not found");
	return found;
}
}

class TestSearchLast{
public static void main(String[]args){


int[]entry={1,8,6,95,4,1,95,5,9,8,95,1};
String userInput=JOptionPane.showInputDialog("Enter the number that you want to search for:");
int number=Integer.parseInt(userInput);

SearchLast.searchLast(entry, number);

System.exit(0);
}
}
#10

اذا كانت الاصناف في ملف واحد... ضعي مؤشر الماوس داخل الصنف TestSearchLast ثم run

والله اعلم.

#11

برضو نفس ال erorr

#12

جربت اني احط الmethod في كلاس وال main في كلاس

وهذا هو كلاس الmethod و ال main للفائدة:

import javax.swing.*;
class SearchLast{

public static boolean searchLast(int[] entry, int number){
	boolean found=false;
	int i=0;
	String indexs="";
	while(i<entry.length){
		if (number==entry){
		found=true;
		indexs=indexs+"	"+i;
	}
		i++;

	}//while

if (found)
	JOptionPane.showMessageDialog(null,number+" is found"+ "\nindex is "+indexs);
	else
	JOptionPane.showMessageDialog(null,number+" is not found");
	return found;
}
}

import javax.swing.*;
class TestSearchLast{
public static void main(String[]args){


int[]entry={1,8,6,95,4,1,95,5,9,8,95,1};
String userInput=JOptionPane.showInputDialog("Enter the number that you want to search for:");
int number=Integer.parseInt(userInput);

SearchLast.searchLast(entry, number);

System.exit(0);
}
}
#13

البرنامج صحيح وشغال

حاول تشغيله عبر سطر الاوامر

java TestSearchLast

منقطع من حين لاخر

#14

أنا عارفه انه شغال

ولكني وضعته للفائدة فقط

#15

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

اعرف اني اطلت الاسئلة ولكني اتمنى ان تتقبلوني بصدر رحب

عندما اضفت للكلاس طلب ال array من المستخدم يظهر لي erorr في الrun ولكني لم اعرف لماذا!

فارجوا مساعتكم

وهذا هو الكلاس:

import javax.swing.*;
class TestSearchLast{
public static void main(String[]args){

String userInput1=JOptionPane.showInputDialog("Enter how many of elements do you want in the array:");
int size=Integer.parseInt(userInput1);

int[] entry=new int[size];

int index;
for(index=0; index<entry.length; index++);{
String userInput2=JOptionPane.showInputDialog("Enter the number:");
entry[index]=Integer.parseInt(userInput2);
}
System.out.print("you have entered:"+entry[index]);

String userInput3=JOptionPane.showInputDialog("Enter the number that you want to search for:");
int number=Integer.parseInt(userInput3);

SearchLast.searchLast(entry, number);

System.exit(0);
}
}

تم تعديل هذه المشاركة بواسطة I love Java في 21 ديسمبر 2006 في 21:48

#16

يومين ولا احد رد على سؤالي

واضح انكم مليتوا من كثرة اسئلتي

#17

السلام عليكم

ماسم الخطأ الي يطلع وقت تنفيذ الكود ؟

أتوقع بأن هناك خطأين الأول ; بعد الfor loop

و الثاني وهو entry[index] حيث أن آخر عنصر قيمه هو entry[index-1]

ولذلك سيعطي الكومبايلر خطأ OutBoundException

لم اجرب الكود ولكن هذا مالاحظته

أتمنى ان تركزين وتعملين تتبع للكود وستلاحظين أن جميع المشاكل انحلت

#18

اقترح نقل حلقة الفور هذه الى موضوع أسئلة في الجافا لوجود عقبة خفية :lol:

حلقة الفور صحيحة جدا ولكن المشكلة في الفاصله المنقوطة ، يجب إزالتها ..

for(index=0; index<entry.length; index++);{

ويبدوا ان اخي عيسى ذكر ذلك في مشاركته السابقة

وشي آخر يجب ادراج امر الطباعة داخل حلقة الفور،

System.out.print("you have entered:"+entry[index]);

تم تعديل هذه المشاركة بواسطة ali7876 في 23 ديسمبر 2006 في 08:45

#19

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

والله يجزاك الف خير

اقتباس
اقترح نقل حلقة الفور هذه الى موضوع أسئلة في الجافا لوجود عقبة خفية :lol:

اساسا هذي من أخطاء الكبار :angry:

ما ينتبهون للأشياء الصغيره -_-

وبعدين انا اكتشفت اخطاء في اوامر الطباعه غير الللي ذكرته

وهذا هو الكلاس بعد التعديل <_<

import javax.swing.*;
class TestSearchLast{
public static void main(String[]args){

String userInput1=JOptionPane.showInputDialog("Enter how many of elements do you want in the array :");
int size=Integer.parseInt(userInput1);

int[] entry=new int[size];

int index;
for(index=0; index<entry.length; index++){
String userInput2=JOptionPane.showInputDialog("Enter the number :");
entry[index]=Integer.parseInt(userInput2);
}

System.out.println("Your array is:");
for(int counter=0; counter<entry.length; counter++){
System.out.print(entry[counter]+" ");
}


String userInput3=JOptionPane.showInputDialog("Enter the number that you want to search for :");
int number=Integer.parseInt(userInput3);


SearchLast.searchLast(entry, number);

System.exit(0);
}
}

هذا الموضوع مغلق.

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